GridView控件:更改按钮文本和链接在页面加载 [英] Gridview: Change Button Text and link on Page load
问题描述
大师,
我与模板字段按钮GridView和我想改变关于在页面加载用户类型的按钮上的文字。
我想从GridView的获得按钮更改文本和链接,但控制总是返回null。这将是真正有助于了解如何改变在页面加载的文本和链接。
TryCast(row.Cells(0).Controls(0).FindControl(idAppearButton),
钮)
块引用>保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load
如果修剪(的Request.QueryString(MSG))<> 然后
如果INSTR(的Request.QueryString(消息),&下;)> 0,则
回复于(中间(的Request.QueryString(消息),1,INSTR(的Request.QueryString(消息),&下;)))
其他
回复于(的Request.QueryString(MSG))
万一
万一
如果Page.IsPostBack = false,那么
昏暗sSeries作为字符串 sSeries =目标NBDE第1部分 昏暗o_cmd作为的SqlCommand
昏暗o_reader作为SqlDataReader的
o_Con =新的SqlConnection(GlobalVarC.DataS)
o_Con.Open() 昏暗的DS作为新Data.DataSet
昏暗达作为SqlDataAdapter的 昏暗ExamId作为字符串
S_Sql =&AMP从Exam_Ser那里系列=SELECT SNO sSeries.ToString&安培; '
o_cmd =新的SqlCommand(S_Sql,o_Con)
o_reader = o_cmd.ExecuteReader
ExamId =
昏暗ExamIdTmp作为字符串
虽然o_reader.Read
如果ExamId.Equals()。然后
ExamId = ExamId +斯诺=
其他
ExamId = ExamId +或SNO =
万一
ExamIdTmp = o_reader(0)的ToString
ExamId = ExamId + ExamIdTmp
ExamId = ExamId +
虽然结束
o_reader.Close()
o_cmd.Dispose() S_Sql =&AMP选择考试,在这里名称; ExamId.ToString DA =新SqlDataAdapter的(S_Sql,o_Con)
da.Fill(DS)
GridSubject.DataSource = DS
GridSubject.DataBind()
da.Dispose()
ds.Dispose()
o_Con.Close() 对于每一行作为GridViewRow在GridSubject.Rows
昏暗的按钮如按钮
按钮= TryCast(row.Cells(0).Controls(0).FindControl(idAppearButton),按钮)
'button.Text =买入
下一个 万一 结束小组< ASP:GridView控件ID =GridSubject=服务器的AutoGenerateColumns =FALSE的cellpadding =3
OnSelectedIndexChanged =GridSubject_SelectedIndexChanged背景色=#CCCCCC
BORDERCOLOR =#E7E7FF边框=无边框宽度=1像素
WIDTH =714px网格=水平
>
< EditRowStyle字体-名称=宋体/>
< EmptyDataRowStyle字体-名称=宋体/>
< FooterStyle背景色=#B5C7DE前景色=#4A3C8CFONT-名称=宋体/>
< AlternatingRowStyle背景色=#F7F7F7FONT-名称=宋体/>
<柱体和GT;
< ASP:BoundField的数据字段=名称的HeaderText =科目名称>
< ItemStyle宽度=140pxHorizontalAlign =左VerticalAlign =顶部/>
< HeaderStyle HorizontalAlign =左/>
< / ASP:BoundField的>
< ASP:的TemplateField ItemStyle-WIDTH =100px的>
<&ItemTemplate中GT;
< ASP:RadioButtonList的ID =RadioButtonList1=服务器>
&所述; asp的:列表项值=0→1部分与所述; / ASP:列表项>
&所述; asp的:列表项值=1→2部分与所述; / ASP:列表项>
< ASP:列表项选择=真值=3大于1部分和放大器;放大器; 2'; / ASP:ListItem的>
< / ASP:RadioButtonList的>
< / ItemTemplate中>
< FooterStyle HorizontalAlign =左/>
< HeaderStyle HorizontalAlign =左/>
< ItemStyle宽度=100px的HorizontalAlign =左>< / ItemStyle>
< / ASP:的TemplateField>
< ASP:的TemplateField ShowHeader =FALSE>
<&ItemTemplate中GT;
< ASP:按钮的ID =idAppearButton=服务器
的CommandName =MYCOMMAND文本=出现>
< / ASP:按钮>
< / ItemTemplate中>
< / ASP:的TemplateField> < ASP:ButtonField字段按钮类型=按钮的CommandName =ViewResults文本=结果>
< HeaderStyle HorizontalAlign =中心VerticalAlign =顶部/>
< / ASP:ButtonField字段> < ASP:的TemplateField的HeaderText =状态>
<&ItemTemplate中GT;
< ASP:标签=服务器文本=未完成>< / ASP:标签>
< / ItemTemplate中>
< / ASP:的TemplateField> < /专栏>
< RowStyle的BackColor =#E7E7FF前景色=#000000FONT-名称=宋体/>
< SelectedRowStyle背景色=#738A9CFONT-粗体=真前景色=#F7F7F7
字体名称=坎布里亚/>
< PagerStyle背景色=#E7E7FF前景色=#4A3C8CHorizontalAlign =右
字体名称=宋体/>
< HeaderStyle背景色=#000000字体粗体=真前景色=#F7F7F7/>
< SortedAscendingCellStyle背景色=#F1F1F1/>
< SortedAscendingHeaderStyle背景色=#808080/>
< SortedDescendingCellStyle背景色=#CAC9C9/>
< SortedDescendingHeaderStyle背景色=#383838/>&所述; SortedAscendingCellStyle背景色=#F4F4FD>&下; / SortedAscendingCellStyle>&所述; SortedAscendingHeaderStyle背景色=#5A4C9D>&下; / SortedAscendingHeaderStyle>&所述; SortedDescendingCellStyle背景色=#D8D8F0>&下; / SortedDescendingCellStyle>&所述; SortedDescendingHeaderStyle背景色=#3E3277>&下; / SortedDescendingHeaderStyle>
< / ASP:GridView的>
解决方案您必须使用
Row.FindControl(ID)
找到参考的控制。不能使用Cell.Text
如果你使用模板列
,只用绑定列工程code>:
按钮= DirectCast(row.FindControl(idAppearButton),按钮)
Gurus,
I have a gridview with the template field button and I want to change text on the button on the user type on page load.
I am trying to get the button from Gridview to change the text and link but the control always return null. It will be really helpful to understand how to change the text and link on page load.
TryCast(row.Cells(0).Controls(0).FindControl("idAppearButton"), Button)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Trim(Request.QueryString("Msg")) <> "" Then If InStr(Request.QueryString("Msg"), "<") > 0 Then Response.Write(Mid(Request.QueryString("Msg"), 1, InStr(Request.QueryString("Msg"), "<"))) Else Response.Write(Request.QueryString("Msg")) End If End If If Page.IsPostBack = False Then Dim sSeries As String sSeries = "Aim NBDE Part 1" Dim o_cmd As SqlCommand Dim o_reader As SqlDataReader o_Con = New SqlConnection(GlobalVarC.DataS) o_Con.Open() Dim ds As New Data.DataSet Dim da As SqlDataAdapter Dim ExamId As String S_Sql = "SELECT SNo from Exam_Ser where Series='" & sSeries.ToString & "'" o_cmd = New SqlCommand(S_Sql, o_Con) o_reader = o_cmd.ExecuteReader ExamId = "" Dim ExamIdTmp As String While o_reader.Read If ExamId.Equals("") Then ExamId = ExamId + "SNo = " Else ExamId = ExamId + " OR SNo = " End If ExamIdTmp = o_reader(0).ToString ExamId = ExamId + ExamIdTmp ExamId = ExamId + "" End While o_reader.Close() o_cmd.Dispose() S_Sql = "SELECT Name from Exam where " & ExamId.ToString da = New SqlDataAdapter(S_Sql, o_Con) da.Fill(ds) GridSubject.DataSource = ds GridSubject.DataBind() da.Dispose() ds.Dispose() o_Con.Close() For Each row As GridViewRow In GridSubject.Rows Dim button As Button 'button = TryCast(row.Cells(0).Controls(0).FindControl("idAppearButton"), Button) 'button.Text = "Buy" Next End If End Sub <asp:GridView ID="GridSubject" runat="server" AutoGenerateColumns="False" CellPadding="3" OnSelectedIndexChanged="GridSubject_SelectedIndexChanged" BackColor="#CCCCCC" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" Width="714px" GridLines="Horizontal" > <EditRowStyle Font-Names="Calibri" /> <EmptyDataRowStyle Font-Names="Calibri" /> <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" Font-Names="Calibri" /> <AlternatingRowStyle BackColor="#F7F7F7" Font-Names="Calibri" /> <Columns > <asp:BoundField DataField="Name" HeaderText="Exam Name"> <ItemStyle Width="140px" HorizontalAlign="Left" VerticalAlign="Top" /> <HeaderStyle HorizontalAlign="Left" /> </asp:BoundField> <asp:TemplateField ItemStyle-Width="100px"> <ItemTemplate> <asp:RadioButtonList ID="RadioButtonList1" runat="server"> <asp:ListItem Value="0">Part 1</asp:ListItem> <asp:ListItem Value="1">Part 2</asp:ListItem> <asp:ListItem Selected="True" Value="3">Part 1 & 2</asp:ListItem> </asp:RadioButtonList> </ItemTemplate> <FooterStyle HorizontalAlign="Left" /> <HeaderStyle HorizontalAlign="Left" /> <ItemStyle Width="100px" HorizontalAlign="Left"></ItemStyle> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:Button ID="idAppearButton" runat="server" CommandName="MYCOMMAND" Text="Appear"> </asp:Button> </ItemTemplate> </asp:TemplateField> <asp:ButtonField ButtonType="Button" CommandName="ViewResults" Text="Results" > <HeaderStyle HorizontalAlign="Center" VerticalAlign="Top" /> </asp:ButtonField> <asp:TemplateField HeaderText="Status"> <ItemTemplate> <asp:Label runat="server" Text="Not Completed"></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <RowStyle BackColor="#E7E7FF" ForeColor="#000000" Font-Names="Calibri" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" Font-Names="Cambria" /> <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" Font-Names="Calibri" /> <HeaderStyle BackColor="#000000" Font-Bold="True" ForeColor="#F7F7F7" /> <SortedAscendingCellStyle BackColor="#F1F1F1" /> <SortedAscendingHeaderStyle BackColor="#808080" /> <SortedDescendingCellStyle BackColor="#CAC9C9" /> <SortedDescendingHeaderStyle BackColor="#383838" /> <SortedAscendingCellStyle BackColor="#F4F4FD"></SortedAscendingCellStyle> <SortedAscendingHeaderStyle BackColor="#5A4C9D"></SortedAscendingHeaderStyle> <SortedDescendingCellStyle BackColor="#D8D8F0"></SortedDescendingCellStyle> <SortedDescendingHeaderStyle BackColor="#3E3277"></SortedDescendingHeaderStyle> </asp:GridView>
解决方案You have to use
Row.FindControl(id)
to find the reference to the control. You cannot useCell.Text
if you useTemplateFields
, that works only withBoundFields
:button = DirectCast(row.FindControl("idAppearButton"), Button)
这篇关于GridView控件:更改按钮文本和链接在页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!