本文介绍了Gridview根据C#变量值绑定列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想根据所选日期在Exx中动态绑定Date列(例如:Day1,Day2等)。 < asp:TemplateField HeaderText = 标题 > < ItemTemplate > < asp:Label ID = LabelRequest runat = server 文字 =' <% #Bind( RequestTitle)%> ' > < / asp:Label > < / ItemTemplate > < ItemStyle 字体粗体 = True Horizo​​ntalAlign = 左 宽度 = 120px Wrap = True / > < / asp:TemplateField > < asp:TemplateField HeaderText = 花费的小时数 ItemStyle-Width = 10% > < ItemTemplate > < asp:TextBox ID = TxtHoursSpent 文字 =' <% #Bind( HoursSpent)%> ' runat = server MaxLength = 4 宽度 = 25 > < / asp:TextBox > < / ItemTemplate > < ItemStyle 宽度 = 10% > < / ItemStyle > 我在c#变量中的价值(Ex:TodayDate)。所以该值与列名匹配。我想在aspx 中添加该值后面的代码Text ='<% #Bind( HoursSpent)%> ')...是它可能??????? 请帮助我......... Thanx:) 这是我的caml查询 qry.Query = @< 其中 > < > < 和 > < > < ; Eq > < FieldRef 名称 =' 资源' / > < 值 类型 =' 整数' > < UserID / > < / Value > < / Eq > < Eq > < FieldRef 名称 =' 年' / > < 价值 类型 =' 文字' > +年份+ @< / Value > < / Eq > < /和 > ; < Eq > < FieldRef 名称 =' 月' / > < 值 类型 =' 文字' > 0+ TimeSheetMonth + @< / Value > < / Eq > < /和 > < Eq > < FieldRef 名称 =' 状态' / > < 值 类型 =' 选择' > 打开< / Value > < / Eq > < /和 > < /其中 > ; qry.ViewFields = @< FieldRef 名称 =' ID' / > < FieldRef 名称 =' RequestID' / > < FieldRef 名称 =' 标题' / > < FieldRef 名称 =' + columnDay +' / > ; qry.ViewFieldsOnly = True; SPListItemCollection listItems = list.GetItems(qry); 如何在aspx中绑定columnDay 解决方案 在您的标记中,您可以执行以下操作: <% #VariableName % > protected void GridView1_OnRowDataBound( object sender,GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { decimal mycolumn = 7 。 50 ; // 我相信小时值应为十进制或整数 TextBox txtHrsSpentColumn =(TextBox)e .Row.FindControl( TxtHoursSpent); txtHrsSpentColumn.Text = mycolumn.ToString(); } } 请检查这是否是您需要的。 如果mycolumn变量的值来自控件,则将其更改为 decimal mycolumn = Convert.ToDecimal (TextBox.Text); I want to bind the Date column dynamically in aspx based on the selected Date (Ex: Day1, Day2 etc).<asp:TemplateField HeaderText="Title"> <ItemTemplate> <asp:Label ID="LabelRequest" runat="server" Text='<%# Bind("RequestTitle") %>'></asp:Label> </ItemTemplate> <ItemStyle Font-Bold="True" HorizontalAlign="Left" Width="120px" Wrap="True" /> </asp:TemplateField> <asp:TemplateField HeaderText="Hours Spent" ItemStyle-Width="10%"> <ItemTemplate> <asp:TextBox ID="TxtHoursSpent" Text='<%# Bind("HoursSpent") %>' runat="server" MaxLength="4" Width="25" ></asp:TextBox> </ItemTemplate> <ItemStyle Width="10%"></ItemStyle>I've the value for tat in c# variable(Ex: TodayDate). so the value matches the column name. I want to add that code behind value in aspxText='<%# Bind("HoursSpent") %>')... is it possible???????Kindly help me.........Thanx :)This is my caml queryqry.Query = @"<Where> <And> <And> <And> <Eq> <FieldRef Name='Resource' /> <Value Type='Integer'> <UserID /> </Value> </Eq> <Eq> <FieldRef Name='Year' /> <Value Type='Text'>" + Year + @"</Value> </Eq> </And> <Eq> <FieldRef Name='Month' /> <Value Type='Text'>0" + TimeSheetMonth + @"</Value> </Eq> </And> <Eq> <FieldRef Name='Status' /> <Value Type='Choice'>Open</Value> </Eq> </And> </Where>";qry.ViewFields = @"<FieldRef Name='ID' /><FieldRef Name='RequestID' /><FieldRef Name='Title'/><FieldRef Name='" + columnDay + "'/>";qry.ViewFieldsOnly = True;SPListItemCollection listItems = list.GetItems(qry);How can i bind the "columnDay" in aspx 解决方案 In your markup you can do this:<%# VariableName %>protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e){ if (e.Row.RowType == DataControlRowType.DataRow) { decimal mycolumn= 7.50; //i believe hours value should be decimal or int TextBox txtHrsSpentColumn = (TextBox)e.Row.FindControl("TxtHoursSpent"); txtHrsSpentColumn.Text = mycolumn.ToString(); }}Please check if this is what you need.if the value of mycolumn variable is coming from a control change it todecimal mycolumn = Convert.ToDecimal(TextBox.Text); 这篇关于Gridview根据C#变量值绑定列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-21 13:56