关于datagrid的使用以及动态修改,以及使用存储过程的介绍

80酷酷网    80kuku.com

  datagrid|存储过程|动态1.关于Datagrid,Datalist等数据绑定控件
由于vs.net是Codebehind的, 而quickstart 上的例子是代码与页面混合的,所以许多朋友有些疑惑。特别是有关template中如何触发事件的问题。
是这样,许多server control都有一个commandname属性
就是在.aspx中设置你那个edit的commandname,在.cs中用e.CommandName 得到

///aspx页面
<asp:DataList id=usersList runat="server" >

<template name="ItemTemplate">
<asp:ImageButton id=ImageButton1 runat="server" ImageUrl="../images/edit.gif" AlternateText="编辑条目" CommandName="edit"></asp:ImageButton>
<asp:ImageButton id=ImageButton2 runat="server" ImageUrl="../images/delete.gif" AlternateText="删除条目" CommandName="delete"></asp:ImageButton>
<asp:Label id=lblRole runat="server" Text="<%# Container.DataItem%>" CssClass="Normal"></asp:Label>
</template>

<template name="EditItemTemplate">
<span class="Normal">email (cookie auth) or DOMAIN\username (Windows auth)</span><br />
<asp:Textbox id=userName width="200" cssclass="NormalTextBox" Text="<%# Container.DataItem %>" runat="server" />  
<asp:LinkButton Text="Apply" CommandName="apply" cssclass="CommandButton" runat="server" ID=ApplyButton/>
</template>

</asp:DataList>



///cs文件
protected void UsersList_ItemCommand (Object sender, DataListCommandEventArgs e)
        {
                if (e.CommandName == "edit") {

                // Set editable list item index if "edit" button clicked next to the item
                usersList.EditItemIndex = e.Item.ItemIndex;

                // Repopulate the datalist control
                BindData();
            }
            else if (e.CommandName == "apply") {

                // Obtain the current portal settings
                PortalConfigurationData portalConfig = PortalConfiguration.LoadSettings(PortalConfiguration.ConfigFilePath);

                // Update user entry
                portalConfig.SecurityRoles[roleIndex].Users[e.Item.ItemIndex] = ((TextBox) e.Item.FindControl("userName")).Text;

                // Persist Settings back to disk
                PortalConfiguration.PersistSettings(portalConfig);

                // Disable editable list item access
                usersList.EditItemIndex = -1;

                // Repopulate the datalist control
                BindData();
            }
            else if (e.CommandName == "delete") {

                // Obtain the current portal settings
                PortalConfigurationData portalConfig = PortalConfiguration.LoadSettings(PortalConfiguration.ConfigFilePath);

                // Delete selected User
                String[] users = new String[portalConfig.SecurityRoles][roleIndex].Users.Length-1];

                for (int i=0, j=0; i < portalConfig.SecurityRoles[roleIndex].Users.Length; i++) {

                    if (i != e.Item.ItemIndex) {

                        users[j] = portalConfig.SecurityRoles[roleIndex].Users[i];
                        j++;
                    }
                }

                // Update Users
                portalConfig.SecurityRoles[roleIndex].Users = users;

                // Persist Settings back to disk
                PortalConfiguration.PersistSettings(portalConfig);

                // Ensure that item is not editable
                usersList.EditItemIndex = -1;

                // Repopulate list
                BindData();
            }
        }
2.关于在asp.net中调用存储过程
例子如下:
SQLConnection myConnection = new SQLConnection(PortalConfiguration.Settings.DatabaseConnectionString);
            SQLCommand myCommand = new SQLCommand("AddMessage", myConnection);

            // Mark the Command as a SPROC
            myCommand.CommandType = CommandType.StoredProcedure;

            // Add Parameters to SPROC
            SQLParameter parameterItemID = new SQLParameter("ItemID", SQLDataType.Int, 4);
            parameterItemID.Direction = ParameterDirection.Output;
            myCommand.Parameters.Add(parameterItemID);

            SQLParameter parameterTitle = new SQLParameter("Title", SQLDataType.NVarChar, 100);
            parameterTitle.Value = title;
            myCommand.Parameters.Add(parameterTitle);

            SQLParameter parameterBody = new SQLParameter("Body", SQLDataType.NVarChar, 3000);
            parameterBody.Value = body;
            myCommand.Parameters.Add(parameterBody);

            SQLParameter parameterParentID = new SQLParameter("ParentID", SQLDataType.Int, 4);
            parameterParentID.Value = parentId;
            myCommand.Parameters.Add(parameterParentID);

            SQLParameter parameterUserName = new SQLParameter("UserName", SQLDataType.NVarChar, 100);
            parameterUserName.Value = userName;
            myCommand.Parameters.Add(parameterUserName);

            SQLParameter parameterPortalID = new SQLParameter("PortalID", SQLDataType.Int, 4);
            parameterPortalID.Value = portalId;
            myCommand.Parameters.Add(parameterPortalID);

            SQLParameter parameterModuleID = new SQLParameter("ModuleID", SQLDataType.Int, 4);
            parameterModuleID.Value = moduleId;
            myCommand.Parameters.Add(parameterModuleID);

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: