2013-09-30 19 views
1

大师,的GridView:ButtonField字段:更改按钮文本和链接在页面加载

而且我的查询上Gridview:TemplateField

我有ButtonField字段一个gridview。我想在页面加载时根据用户类型更改文本。

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 = DirectCast(row.FindControl("idAppearButton"), Button) 
       button.Text = "Buy" 
      Next 

     End If 

    End Sub 

我可以在按钮点击从GridSubject_SelectedIndexChanged执行操作。

我想从GridView的按钮来改变文本。我尝试了一些搜索,并找到了从'GridSubject_SelectedIndexChanged'点击控制按钮的方法,但不是在页面加载时。

如果我可以在页面加载时获取ButtonField CommandName =“ViewResults”的句柄指针,这将会非常有帮助。

    <asp:ButtonField ButtonType="Button" CommandName="ViewResults" Text="Results" > 
           <HeaderStyle HorizontalAlign="Center" VerticalAlign="Top" /> 
        </asp:ButtonField> 


<div id="gridViewId" align="center"> 
    <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 &amp; 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> 
        </div> 
+1

你不能访问它,但你可以用做rowbound事件,每当你绑定到gridview的数据时它会被触发 –

+0

似乎我有2个选项将ButtonFiled转换为TemplateField或使用rowbound事件。如果我可以在rowbound事件上获取样本的指针将会很有帮助。 – Tectrendz

+1

我主要喜欢它的简单模板,因此karl已经发布了sndpet示例代码rowdatabound –

回答

1

使用RowDataBound事件网格视图的每行的工作,因为它绑定到网格,像这样的:在页面加载

Protected Sub gridViewId_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     ' Find the button to change the text of 
     Dim theButtonToChangeTextOf As Button = CType(e.Row.FindControl("idAppearButton"), Button) 
     theButtonToChangeTextOf.Text = theTextYouWantForTheButtonHere 
    End If 
End Sub 
相关问题