我有formview控件。在formview控件里面他们是一个gridview。在gridview中我有FooterTemplate(在footertemplate我有文本框控件来添加文本)来添加文本。为了找到那个控件,我编写了如下代码:
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 :(得分:0)
来自评论:
你以前调用过FormView.DataBind / GridView.DataBind吗?
还要记住FormView有3种不同的FormViewModes。如果它当前处于编辑模式并且GridView位于ReadOnly ItemTemplate中,则您无法访问它。
尝试使用LinkButton的click-event处理程序获取TextBox:
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的DataBound事件中:
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
,即使数据源为DataBind
,您也必须null
!
答案 1 :(得分: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();
}
}