与ASP.NET中的GridView绑定问题

时间:2012-03-07 21:33:20

标签: asp.net gridview

我对ASP.NET GridView绑定的工作方式感到困惑。

我有GridView。现在,在页面加载(使用!IsPostBack)上,我绑定了GridView

我的gridview有一个编辑按钮。当我点击它时,GridView变为空白。可以预期行为,因为当我单击编辑按钮时,会发生回发,并且因为我已将GridView绑定在!IsPostback条件内,所以它不会绑定GridView

现在,如果我删除GridView绑定,并将其置于!IsPostback条件之外,则编辑按钮可以正常工作。但是,我无法从TextBox获取编辑后的值。在这种情况下,也可以预期行为,因为单击更新按钮时,GridView被重新绑定,因为此时的绑定已在!IsPostback条件之外完成。

所以,我想知道编辑按钮的正常代码是什么,同时可以检索TextBox中编辑的值。

问题已更新为代码:

<asp:GridView ID="grdExternalLinkSection1" ShowFooter="true" runat="server" Width="100%" AutoGenerateColumns="false" CellPadding="5" EnableViewState="true">
                                <EmptyDataTemplate>
                                    External Link Title
                                    <asp:TextBox ID="txtExternalLinkTitleEmptySection1" runat="server"></asp:TextBox>
                                    External Link Url
                                    <asp:TextBox ID="txtExternalLinkUrlEmptySection1" runat="server"></asp:TextBox>
                                    <asp:Button ID="btnExternalLinkEmptySection1" OnCommand="grdExternalLinkSection_Button_Clicks" runat="server" Text="Add" CommandName="headernew,1" style="padding:3px; width:56px;" />
                                </EmptyDataTemplate>
                                <Columns>
                                    <asp:TemplateField HeaderText="Title">
                                        <ItemTemplate>
                                            <asp:Label ID="lblExternalLinkTitleSection1" runat="server"><%# Eval("Title") %></asp:Label>
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:TextBox ID="txtExternalLinkTitleEditSection1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>
                                        </EditItemTemplate>
                                        <FooterTemplate>
                                            <asp:TextBox ID="txtExternalLinkTitleFooterSection1" runat="server"></asp:TextBox>
                                        </FooterTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Url">
                                        <ItemTemplate>
                                            <asp:Label ID="lblExternalLinkUrlSection1" runat="server"><%# Eval("Url")%></asp:Label>
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:TextBox ID="txtExternalLinkUrlEditSection1" runat="server" Text='<%# Bind("Url") %>'></asp:TextBox>
                                        </EditItemTemplate>
                                        <FooterTemplate>
                                            <asp:TextBox ID="txtExternalLinkUrlFooterSection1" runat="server"></asp:TextBox>
                                        </FooterTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderText="Manage">
                                        <ItemTemplate>
                                            <asp:Button ID="btnExternalLinkEditSection1" OnCommand="grdExternalLinkSection_Button_Clicks" runat="server" CommandName="Editing,1" CommandArgument='<%# Container.DataItemIndex %>' Text="Edit" />
                                            <asp:Button ID="btnExternalLinkDeleteSection1" OnCommand="grdExternalLinkSection_Button_Clicks" runat="server" CommandName="Deleting,1" CommandArgument='<%# Container.DataItemIndex %>' Text="Delete" />
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:Button ID="btnExternalLinkUpdateSection1" OnCommand="grdExternalLinkSection_Button_Clicks" runat="server" CommandName="Updating,1" CommandArgument='<%# Container.DataItemIndex %>' Text="Update" />
                                            <asp:Button ID="btnExternalLinkCancelSection1" OnCommand="grdExternalLinkSection_Button_Clicks" runat="server" CommandName="Canceling,1" CommandArgument='<%# Container.DataItemIndex %>' Text="Cancel" />
                                        </EditItemTemplate>
                                        <FooterTemplate>
                                            <asp:Button ID="btnExternalLinkAddFooterSection1" OnCommand="grdExternalLinkSection_Button_Clicks" runat="server" CommandName="Footer,1" Text="Add" />
                                        </FooterTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
下面的

是执行绑定工作的函数:

 GridView grid;
    protected void BindExternalLinks(int SectionID, string ControlName)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter("user_Newsletter_GetExternalLinks", connection))
            {
                adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                adapter.SelectCommand.Parameters.Add("@SectionID", SqlDbType.Int).Value = SectionID;
                adapter.SelectCommand.Parameters.Add("@PortalID", SqlDbType.Int).Value = PortalID;
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                grid = (GridView)this.FindControl(ControlName);
                grid.DataSource = dt;
            }
        }
    }
    protected void BindAllExternalLinks()
    {
        for (int i = 1; i <= NewsLetterSectionCount; i++)
        {
            BindExternalLinks(i, "grdExternalLinkSection" + i);
            grid.DataBind();
        }
    }
下面的

是我的PageLoad:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindAllExternalLinks();
        }
    }

以下是我的命令按钮事件:我保持所有命令按钮的处理程序通用:

   protected void grdExternalLinkSection_Button_Clicks(object sender, CommandEventArgs e)
    {
        int rowIndex = (e.CommandArgument != "") ? Convert.ToInt32(e.CommandArgument) : -1;
        string[] commandNames = e.CommandName.ToString().Split(',');
        string command = commandNames[0].ToString().ToLower();
        int sectionID = Convert.ToInt32(commandNames[1]);
        GridView grid = (GridView)this.FindControl("grdExternalLinkSection" + sectionID);
        try
        {
            if (command == "headernew")
            {
                TextBox title = grid.Controls[0].Controls[0].FindControl("txtExternalLinkTitleEmptySection" + sectionID) as TextBox;
                TextBox url = grid.Controls[0].Controls[0].FindControl("txtExternalLinkUrlEmptySection" + sectionID) as TextBox;
                UpdateExternalLinks(ModifyExternalLinks.Insert, sectionID, title.Text, url.Text);
                MessageShow("External Link Added Successfully");
            }
            else if (command == "editing")
            {
                //grid.EditIndex = rowIndex;
            }
            else if (command == "canceling")
            {
                grid.EditIndex = -1;
            }
            else if (command == "footer")
            {
                Response.Write("Inside Footer");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        BindExternalLinks(sectionID, "grdExternalLinkSection" + sectionID);
        grid.DataBind(); //here i am binding once the records are modified.
    }

2 个答案:

答案 0 :(得分:1)

你快到了。 BindAllExternalLinks()应位于if (!IsPostback)块内,正确。

您应该做的另一件事是在完成编辑后重新绑定网格:

else if (command == "editing")
{
    // do your update stuff here

    BindAllExternalLinks();
}

答案 1 :(得分:0)

只需重新绑定每个回发事件()下的gridview。即使将它绑定到对象数据源或SQL数据源,也必须在每次回发请求之后调用databind方法,如

              MyGridview.DataBind();
              UpdatePanel1.Update();