如何在c#中读取项模板字段
本文关键字:字段 读取 | 更新日期: 2023-09-27 18:16:40
在项目模板中有一个下拉框。根据我的要求,我需要更新下拉在aspx.cs页面的工具提示字段。
我使用以下代码:
<asp:TemplateField HeaderStyle-CssClass="grid-label-small" HeaderText="*State">
<ItemTemplate>
<asp:DropDownList ID="ddlDefState" Width="110px" runat="server" ToolTip="Select State">
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle CssClass="grid-label-small" />
</asp:TemplateField>`.
谢谢…
嗨,你可以使用这个找到Item模板控件,假设你绑定在RowDataBound事件中。像这样给GridView添加OnRowDataBound事件,
<asp:GridView runat="server" OnRowDataBound="grd_RowDataBound" ></asp:GridView>
protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList dropDown;
dropDown= e.Row.FindControl("ddlDefState") as DropDownList;
if (dropDown!= null)
// Write your code here
}
}
可选的,你也可以通过这种方式绑定到asp:GridView标记的工具提示,
<asp:TemplateField HeaderStyle-CssClass="grid-label-small" HeaderText="*State">
<ItemTemplate>
<asp:DropDownList ID="ddlDefState" Width="110px" runat="server"
ToolTip="<%# Bind("Your Property Name") %>">
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle CssClass="grid-label-small" />
</asp:TemplateField>
希望这对你有帮助。由于
使用GridView的rowdataboundevent:
void TitleGridView_RowDataBound (Object sender, GridViewRowEventArgs e)
{
// Get the RadioButtonList control from the row.
RadioButtonList radio = (RadioButtonList)e.Row.FindControl("TypeList");
// To Get DropDownList Cast as DropDownList and Find with it's Id - ddlDefState
DropDownList list = (DropDownList )e.Row.FindControl("ddlDefState");
检查这个TemplateField。
处理GridView的事件RowDataBound
:
<asp:GridView runat="server" id="gvData" OnRowDataBound="gvData_RowDataBound">
<HeaderStyle CssClass="grid-label-small" />
<Columns>
<asp:TemplateField HeaderStyle-CssClass="grid-label-small" HeaderText="*State">
<ItemTemplate>
<asp:DropDownList ID="ddlDefState" Width="110px" runat="server"
ToolTip="Select State">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
现在,在事件gvData_RowDataBound
中找到dropdown
并将tooltip
更改为:
protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlDefState;
ddlDefState = (DropDownList)e.Row.FindControl("ddlDefState");
if (ddlDefState != null)
{
ddlDefState.ToolTip = "Your New Tooltips";
// Write your other code here
}
}
}