正在从模板字段内的控件中读取有界DataItem表达式文本
本文关键字:读取 DataItem 文本 表达式 控件 字段 | 更新日期: 2023-09-27 17:57:29
我有一个自定义的网格视图,在太多的页面中使用该网格,我想在后面的代码中为该网格内的所有列设置SortExpression文本;正如您在下面的代码中看到的那样,我通过重写OnRowDataBound事件处理程序来实现这一点。
问题是,我无法像在BoundField 中那样访问模板字段的评估数据字段表达式文本(在我的情况下为"START_WORK_DATE")
<eska:GridView ID="gvEmployees" runat="server" CssClass="new-grid" PageSize="20"
AllowSorting="true" AllowPaging="True"
AutoGenerateColumns="False" Width="100%"
GridContainer="tdEmployees" DataKeyNames="ID"
RowHoverCssClass="HoverRow" ShowCheckBox="True"
OnPageIndexChanging="gvEmployees_PageIndexChanging"
OnSelectedIndexChanged="gvEmployees_SelectedIndexChanged"
meta:resourcekey="gvEmployeesResource1">
<PagerSettings FirstPageImageUrl="~/App_Themes/Images/First.gif" LastPageImageUrl="~/App_Themes/Images/last.gif"
Mode="NextPreviousFirstLast" NextPageImageUrl="~/App_Themes/Images/Next.gif"
PreviousPageImageUrl="~/App_Themes/Images/prev.gif"></PagerSettings>
<SelectedRowStyle CssClass="SelectedRow"></SelectedRowStyle>
<Columns>
<asp:BoundField meta:resourcekey="BoundFieldResource1">
<ItemStyle Width="2%" />
<HeaderStyle Width="2%" />
<FooterStyle Width="2%" />
</asp:BoundField>
<asp:BoundField DataField="EMP_NO" HeaderText="Employee No"
meta:resourcekey="BoundFieldResource3">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="10%"></ItemStyle>
<FooterStyle Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="NAME" HeaderText="Name" meta:resourcekey="BoundFieldResource4">
<HeaderStyle Width="34%"></HeaderStyle>
<ItemStyle Width="34%"></ItemStyle>
<FooterStyle Width="34%" />
</asp:BoundField>
<asp:TemplateField HeaderText="Hiring Date" meta:resourcekey="TemplateFieldResource1">
<ItemTemplate>
<asp:Label runat="server"Text='<%# Convert.ToDateTime(DataBinder.Eval(Container,
"DataItem.START_WORK_DATE")).ToString("dd-MM-yyyy") %>'
ID="lblHiringDateGrid"></asp:Label>
</ItemTemplate>
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="10%"></ItemStyle>
<FooterStyle Width="10%" />
</asp:TemplateField>
<asp:BoundField DataField="PHONE1" HeaderText="Phone 1"
meta:resourcekey="BoundFieldResource5">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="10%"></ItemStyle>
<FooterStyle Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="Position_Name" HeaderText="Position"
meta:resourcekey="BoundFieldResource6">
<HeaderStyle Width="20%"></HeaderStyle>
<ItemStyle Width="20%"></ItemStyle>
<FooterStyle Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="STATUS_DESC" HeaderText="Status"
meta:resourcekey="BoundFieldResource7">
<HeaderStyle Width="12%"></HeaderStyle>
<ItemStyle Width="12%"></ItemStyle>
<FooterStyle Width="12%" />
</asp:BoundField>
<asp:BoundField meta:resourcekey="BoundFieldResource8">
<ItemStyle Width="2%" />
<HeaderStyle Width="2%" />
<FooterStyle Width="2%" />
</asp:BoundField>
</Columns>
</eska:GridView>
代码背后:
protected override void OnRowDataBound(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < this.Columns.Count; i++)
{
if (this.Columns[i] is BoundField)
{
if (((BoundField)(this.Columns[i])).DataField != string.Empty)
{
this.Columns[i].SortExpression = ((BoundField)(this.Columns[i])).DataField;
}
}
else if (this.Columns[i] is TemplateField)
{
// ???
}
}
}
}
您可以这样做。使用链接按钮设置标题模板。
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="LinkButtonEmpName" runat="server" Text="Employee Name" CommandName="Sort" CommandArgument="Employees">
</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="LabelEmployee" runat="server" Text='<%# Bind("Employees") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBoxEmployee" runat="server"/>
</FooterTemplate>
行命令中的事件查找命令如下。
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e){
if (e.CommandName.Equals("Sort"))
{
FilterExpression = e.CommandArgument.ToString() + " LIKE '%" + txtCaseNumber.Text + "%'";
BindGridView();
}}
请参阅此处的完整示例。
http://www.codeproject.com/Questions/528664/HowplustoplusapplyplussortingplusinplusTemplateplu