2011-08-09 29 views
1

我有formview控件。内部formview控制它们是一个gridview。在gridview我有FooterTemplate(在footertemplate我有文本框控件添加文本)添加文本。要找到控制我写的代码如下:关于在asp.net里面formview中查找gridview里面的页脚控件

GridView mygridview = (GridView)FVAddCustomer.FindControl("mygridview"); 
TextBox txtFName1 = (TextBox)mygridview.FooterRow.FindControl("txtFName1"); 

这是对的还是其他方式?因为当我找到文本框值,那么它变为空? 请帮帮我吗?

<asp:GridView ID="mygridview" runat="server" AutoGenerateColumns="False" ShowFooter="True" BackColor="White" BorderColor="#336666" BorderWidth="3px" CellPadding="4" GridLines="Horizontal" BorderStyle="Double" Width="100%" OnRowEditing="mygridview_RowEditing" OnRowCancelingEdit="mygridview_RowCancelingEdit" OnRowUpdating="mygridview_RowUpdating" Visible="false"> 
     <FooterStyle BackColor="White" ForeColor="#333333" /> 
     <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" /> 
     <SelectedRowStyle BackColor="#339966" ForeColor="White" Font-Bold="True" /> 
     <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" /> 
     <Columns> 
      <asp:TemplateField HeaderText="First Name"> 
      <ItemTemplate> 
       <asp:Label ID="lblFName1" runat="server" Text='<%#Eval("FirstName")%>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
        <asp:TextBox ID="txtFName1Edit" runat="server" Width="90px"></asp:TextBox> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:TextBox ID="txtFName1" runat="server" Width="90px"></asp:TextBox> 
      </FooterTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Edit"> 
      <ItemTemplate> 
       <asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" CausesValidation="false" OnClick="lnkEdit_Click"></asp:LinkButton> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CommandName="Update"></asp:LinkButton> 
       <asp:LinkButton ID="lnkCancel" runat="server" Text="cancel" CommandName="Cancel"></asp:LinkButton> 
      </EditItemTemplate> 
      <FooterTemplate> 
       <asp:LinkButton ID="lnkAdd" runat="server" Text="Add" CommandName="Add" Width="90px" OnClick="lnkAdd_Click"></asp:LinkButton> 
      </FooterTemplate> 
     </asp:TemplateField> 

谢谢。 Asp.net C#

+0

你在哪里调用此代码插入操作?你之前是否调用过FormView.DataBind/GridView.DataBind? –

+0

是的,我已经在页面加载中调用了gridview.databind – abc

+0

你还没有回答我的问题,你试图获得'txtFName1'。还记得FormView有3个不同的FormViewModes。如果它当前处于编辑模式,并且GridView位于ReadOnly ItemTemplate中,则无法访问它。 –

回答

0

从评论:

你有叫FormView.DataBind/GridView.DataBind过吗?

还请记住,FormView有3个不同的FormViewModes。如果它当前处于编辑模式,并且GridView位于ReadOnly ItemTemplate中,则无法访问它。

试试这个摆脱LinkBut​​ton的点击事件处理程序的文本框:

private void lnkadd_Click(object sender, System.EventArgs e) 
{ 
    var footer = (GridViewRow)((WebControl)sender).NamingContainer; 
    var txtFName1 = (TextBox)footer.FindControl("footer"); 
} 

编辑:如果这不起作用,你可以尝试处理GridView's RowCommand

protected void mygridview_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Add") { 
     var txtFName1 = (TextBox)((GridView)sender).FooterRow.FindControl("txtFName1"); 
    } 
} 

EDIT2:

只有在!IsPostBack的情况下,您才应该将GridView绑定到它的DataSource。要做到这一点,最好的地方是在FormView的数据绑定事件:

protected void FormView1_DataBound(object sender, System.EventArgs e) 
{ 
    switch (FormView1.CurrentMode) { 
     case FormViewMode.Insert: 
      var mygridview = (GridView)((FormView)sender).FindControl("mygridview"); 
      //Bind your GridView here' 
      break; 
    } 
} 

在这种方式可以确保当FormView控件被数据绑定GridView控件会自动反弹。如果FormView将其模式更改为Insert,则即使数据源为null,也必须使用DataBind

+0

我使用这段代码,但txtFName1值为空(它没有找到txtFName1) – abc

+0

@abc:那么你应该向我们展示GridView的aspx标记。 –

+0

我在我的问题中发布了我的标记,请参阅它并建议做任何更改 – abc

0

我发现只有在pageLoad事件中发生(!IsPostback)时,才会填充网格。就像这样:

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

,然后我进行这是成功的

protected void CustomerGridview_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
     bool flag = false; 
     if (e.CommandName.Equals("Insert")) 
     { 
      TextBox txtNewName = (TextBox)CustomerGridview.FooterRow.FindControl("txtNewName");     
      DropDownList ddlNewGender = (DropDownList)CustomerGridview.FooterRow.FindControl("ddlNewGender"); 
      DropDownList ddlNewType = (DropDownList)CustomerGridview.FooterRow.FindControl("cmbNewType"); 
      CheckBox chkNewActive = (CheckBox)CustomerGridview.FooterRow.FindControl("chkNewActive"); 
      if (chkNewActive.Checked) flag = true; 
      else 
       flag = false; 
      SqlConnection con = new SqlConnection(conection); 
      con.Open(); 
      SqlCommand command = con.CreateCommand(); 
      SqlTransaction transaction; 
      // Start a local transaction. 
      transaction = con.BeginTransaction(); 
      // Must assign both transaction object and connection 
      // to Command object for a pending local transaction 
      command.Connection = con; 
      command.Transaction = transaction; 

      try 
      { 
       command.CommandText = "Insert into Contact (Name,Sex,Type,IsActive) values('" + txtNewName.Text + "','" + ddlNewGender.SelectedValue.ToString() + "','" + ddlNewType.SelectedValue.ToString() + "','" + flag + "')"; 
       command.ExecuteNonQuery(); 
       transaction.Commit(); 
       Message.Text = "Record has been saved!!"; 
      } 
      catch (Exception ex) 
      { 
       Message.Text = "Error Occured!!"; 
       try 
       { 
        transaction.Rollback(); 
       } 
       catch(Exception ex2) 
       { 
        Message.Text = "Transaction RollBack!"; 
       } 
      } 
      FillGrid(); 
     } 
    }