积极答复者
Button的OnClick事件里找FormView里的控件

问题
-
问题描述:页面有一个Button,一个FormView,一个TextBox。现在我在Button的OnClick事件里需要找FormView里Insert模板里的TextBox控件。代码如下:
this.fvSingle.ChangeMode(FormViewMode.Edit);
TextBox txt = (TextBox)this.fvSingle.FindControl("INatureTextBox");
发现这样写找不到TextBox控件。我试过用this.fvSingle.Row.FindControl("INatureTextBox");也不行,而且直接报未将对象引用到实例。真是急死人了。
请教各位高人,如果找到TextBox呢?在线等!,先谢谢各位先:)
zy
答案
-
但我的问题是:btnEdit_Click只可能执行一次,怎么可能执行两次呢?还有:this.fvSingle.ChangeMode(FormViewMode.Edit);不是改变FormView的模板状态么?为什么不会直接改掉?而是要等到第二次后才会改?
zy
你这么想的话 那是Windows了 不是web了 你确实执行this.fvSingle.ChangeMode(FormViewMode.Edit);了 因为它的模板还没生成出来 你去哪找控件 第二次按钮时候正好生成编辑模板完成包括视图提交服务端上- 已标记为答案 张烨 2009年9月14日 15:48
全部回复
-
#region 编辑指标按钮
protected void btnEdit_Click(object sender, EventArgs e)
{
this.odsSingle.SelectParameters[0].DefaultValue = this.hfId.Value;
this.odsSingle.DataBind();
this.fvSingle.ChangeMode(FormViewMode.Edit);
if(this.fvSingle.Row.RowState == DataControlRowState.Edit)
{
TextBox txt = (TextBox)this.fvSingle.FindControl("INatureTextBox");
}
this.wdwIndicators.Header.CaptionText = "编辑指标";
this.wdwIndicators.Header.Font.Size = 10;
this.wdwIndicators.WindowState = DialogWindowState.Normal;
}
#endregion
<EditItemTemplate>
<div id="divFv" style="width:300px; margin:10px">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
<span>指标类型:</span>
<asp:TextBox ID="ITypeTextBox" runat="server" Text='<%# Bind("IType") %>' ReadOnly="true" CssClass="noTextBox"/>
</td>
</tr>
<tr>
<td>
<span>指标性质:</span>
<asp:TextBox ID="INatureTextBox" runat="server" Text='<%# Bind("INature") %>' ReadOnly="true" CssClass="noTextBox" />
</td>
</tr>
<tr id="trUnits" runat="server" visible="false">
<td>
<span>指标单位:</span>
<asp:DropDownList ID="ddlUnits" runat="server" CssClass="dropdownlist" SelectedValue='<%# Bind("IUnits") %>'>
<asp:ListItem Text="" Value="" />
<asp:ListItem Text="%" Value="%" />
<asp:ListItem Text="万元" Value="万元" />
<asp:ListItem Text="元/万元" Value="元/万元" />
<asp:ListItem Text="人/万元" Value="人/万元" />
<asp:ListItem Text="KWH/万元" Value="KWH/万元" />
<asp:ListItem Text="吨/万元" Value="吨/万元" />
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<span>指标名称:</span>
<asp:TextBox ID="INameTextBox" runat="server" Text='<%# Bind("IName") %>' CssClass="textbox" onfocus="OverTextBox(this)" onfocusout="OutTextBox(this)" />
</td>
</tr>
</table>
</div>
<div class="divLbtn">
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="更新" OnClientClick="return ValidData();" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" />
<asp:TextBox ID="IdTextBox" runat="server" Text='<%# Bind("Id") %>' CssClass="txtNoDisplay" />
</div>
</EditItemTemplate>
zy -
点击两次按钮?这个怎么实现?我不是太明白。希望您的指导
zy
protected void btnEdit_Click(object sender, EventArgs e)
{
this.odsSingle.SelectParameters[0].DefaultValue = this.hfId.Value;
this.odsSingle.DataBind();
this.fvSingle.ChangeMode(FormViewMode.Edit);
if(this.fvSingle.Row.RowState == DataControlRowState.Edit)
{
TextBox txt = (TextBox)this.fvSingle.FindControl("INatureTextBox");
}
this.wdwIndicators.Header.CaptionText = "编辑指标";
this.wdwIndicators.Header.Font.Size = 10;
this.wdwIndicators.WindowState = DialogWindowState.Normal;
} 你的代码情况是你当点击第一次按钮 先 执行了 this.fvSingle.ChangeMode(FormViewMode.Edit);
执行 if(this.fvSingle.Row.RowState == DataControlRowState.Edit)还没打开状态的 这个条件不正确 就返回客户端了
当你按钮点击第二次时候 已经是编辑状态 那 if(this.fvSingle.Row.RowState == DataControlRowState.Edit) 才为真- 已建议为答案 mldark 2009年9月14日 15:00
-
但我的问题是:btnEdit_Click只可能执行一次,怎么可能执行两次呢?还有:this.fvSingle.ChangeMode(FormViewMode.Edit);不是改变FormView的模板状态么?为什么不会直接改掉?而是要等到第二次后才会改?
zy
你这么想的话 那是Windows了 不是web了 你确实执行this.fvSingle.ChangeMode(FormViewMode.Edit);了 因为它的模板还没生成出来 你去哪找控件 第二次按钮时候正好生成编辑模板完成包括视图提交服务端上- 已标记为答案 张烨 2009年9月14日 15:48
-
那我如何才能实现您所说得按两次按钮呢?
zy
你的代码难道不是吗?
我的代码和你的类似的
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1">
<EditItemTemplate>
id:
<asp:Label ID="idLabel1" runat="server" Text='<%# Eval("id") %>' />
<br />
pid:
<asp:TextBox ID="e" runat="server" Text='<%# Bind("pid") %>' />
<br />
imagename:
<asp:TextBox ID="imagenameTextBox" runat="server"
Text='<%# Bind("imagename") %>' />
<br />
dtime:
<asp:TextBox ID="dtimeTextBox" runat="server" Text='<%# Bind("dtime") %>' />
<br />
title:
<asp:TextBox ID="titleTextBox" runat="server" Text='<%# Bind("title") %>' />
<br />
albumcontent:
<asp:TextBox ID="albumcontentTextBox" runat="server"
Text='<%# Bind("albumcontent") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="更新" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="取消" />
</EditItemTemplate>
<InsertItemTemplate>
pid:
<asp:TextBox ID="pid" runat="server" Text='<%# Bind("pid") %>' />
<br />
imagename:
<asp:TextBox ID="imagenameTextBox" runat="server"
Text='<%# Bind("imagename") %>' />
<br />
dtime:
<asp:TextBox ID="dtimeTextBox" runat="server" Text='<%# Bind("dtime") %>' />
<br />
title:
<asp:TextBox ID="titleTextBox" runat="server" Text='<%# Bind("title") %>' />
<br />
albumcontent:
<asp:TextBox ID="albumcontentTextBox" runat="server"
Text='<%# Bind("albumcontent") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="插入" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="取消" />
</InsertItemTemplate>
<ItemTemplate>
id:
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
<br />
pid:
<asp:Label ID="pidLabel" runat="server" Text='<%# Bind("pid") %>' />
<br />
imagename:
<asp:Label ID="imagenameLabel" runat="server" Text='<%# Bind("imagename") %>' />
<br />
dtime:
<asp:Label ID="dtimeLabel" runat="server" Text='<%# Bind("dtime") %>' />
<br />
title:
<asp:Label ID="titleLabel" runat="server" Text='<%# Bind("title") %>' />
<br />
albumcontent:
<asp:Label ID="albumcontentLabel" runat="server"
Text='<%# Bind("albumcontent") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit" Text="编辑" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
CommandName="Delete" Text="删除" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="新建" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:blogsConnectionString %>"
DeleteCommand="DELETE FROM [albumcontent] WHERE [id] = @original_id AND [pid] = @original_pid AND [imagename] = @original_imagename AND [dtime] = @original_dtime AND [title] = @original_title AND (([albumcontent] = @original_albumcontent) OR ([albumcontent] IS NULL AND @original_albumcontent IS NULL))"
InsertCommand="INSERT INTO [albumcontent] ([pid], [imagename], [dtime], [title], [albumcontent]) VALUES (@pid, @imagename, @dtime, @title, @albumcontent)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [albumcontent]"
UpdateCommand="UPDATE [albumcontent] SET [pid] = @pid, [imagename] = @imagename, [dtime] = @dtime, [title] = @title, [albumcontent] = @albumcontent WHERE [id] = @original_id AND [pid] = @original_pid AND [imagename] = @original_imagename AND [dtime] = @original_dtime AND [title] = @original_title AND (([albumcontent] = @original_albumcontent) OR ([albumcontent] IS NULL AND @original_albumcontent IS NULL))">
<DeleteParameters>
<asp:Parameter Name="original_id" Type="Int32" />
<asp:Parameter Name="original_pid" Type="Int32" />
<asp:Parameter Name="original_imagename" Type="String" />
<asp:Parameter Name="original_dtime" Type="DateTime" />
<asp:Parameter Name="original_title" Type="String" />
<asp:Parameter Name="original_albumcontent" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="pid" Type="Int32" />
<asp:Parameter Name="imagename" Type="String" />
<asp:Parameter Name="dtime" Type="DateTime" />
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="albumcontent" Type="String" />
<asp:Parameter Name="original_id" Type="Int32" />
<asp:Parameter Name="original_pid" Type="Int32" />
<asp:Parameter Name="original_imagename" Type="String" />
<asp:Parameter Name="original_dtime" Type="DateTime" />
<asp:Parameter Name="original_title" Type="String" />
<asp:Parameter Name="original_albumcontent" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="pid" Type="Int32" />
<asp:Parameter Name="imagename" Type="String" />
<asp:Parameter Name="dtime" Type="DateTime" />
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="albumcontent" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
protected void Button1_Click(object sender, EventArgs e)
{
FormView1.ChangeMode(FormViewMode.Edit);
if (FormView1.Row.RowState == DataControlRowState.Edit)
{
TextBox t = FormView1.FindControl("e") as TextBox;
Response.Write("111");
}
}