2012-03-07 61 views
0

我完全与ASP.NET GridView绑定工作方式混淆。我有一个GridView。现在,在页面加载(使用!IsPostBack)我绑定GridView在ASP.NET中使用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. 
    } 
+0

每次回传请求后,请您的文章分成几段,让他们不那么难读。另外,如果可能,请尝试使用正确的大小写和标点符号。 – jadarnel27 2012-03-07 21:43:42

+0

向我们展示您的GridView aspx标记。什么意思_blank_并启用了ViewState? – 2012-03-07 21:43:47

+0

你还没有显示整个GridView,我错过了CommandName =“editing”的按钮。从哪里调用BindExternalLinks? – 2012-03-07 21:53:47

回答

1

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

你应该做的事情更多的是重新绑定网格,你做你的编辑后:

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

    BindAllExternalLinks(); 
} 
0

只需重新绑定每次回发事件下的GridView()。即使你把它绑定到一个对象的数据源或SQL数据源,你必须调用数据绑定方法类似

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