gridview复选框和按钮单击事件 [英] gridview check box and button click events

查看:90
本文介绍了gridview复选框和按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要获取该列的值,该列是复选框上的模板字段以及gridview的按钮单击事件

我有gridview控件,有3列用于行
第一列具有复选框
第二列字符串值
和第三列按钮
当我选中复选框时,我希望在按钮上显示第二列的值.
谁能给我建议解决方案

谢谢inadvance

解决方案

通过使用此代码,您具有单击了该按钮的gridview行.
在按钮的onclick事件中:

Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;



通过使用此代码,您可以在gridview行中单击该复选框.
在复选框的onselectedChanged事件中:

CheckBox chkbx = (CheckBox)sender;
GridViewRow row = (GridViewRow)chkbx.NamingContainer;
if (chkbx.Checked)
{
 /// your code here to do with the textbox what ever you need to do.

}



我无法给出更精确的答案,原因是我不知道复选框或按钮是否在第2列中显示了值

  <asp:GridView ID="grdFmlHist" runat="server" AllowPaging="True" Enabled ="False"
    AutoGenerateColumns="False" DataKeyNames="Relationship" Height="40px" WIDTH="450px"
    PageSize="8" ShowFooter="True"  onrowdatabound="grdFmlHist_RowDataBound" >
    <PagerSettings Mode="NumericFirstLast" />
    <RowStyle BackColor="WhiteSmoke" BorderColor="CornflowerBlue" ForeColor="Black"
        Height="30px" />
    <Columns>
    <asp:TemplateField HeaderText="S.No">
          <ItemTemplate>
              <asp:CheckBox ID="chkSelect" runat="server" />
        </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="S.No">
          <ItemTemplate>
            <%# ((GridViewRow)Container).RowIndex + 1%>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Relationship" HeaderText="Relationship" />
        <asp:BoundField DataField="Disease" HeaderText="Disease" />
        <asp:BoundField DataField="Dates" HeaderText="Dates" />
        <asp:TemplateField>
        <ItemTemplate>
            <asp:Button ID="btnDisplay" runat="server"  /></ItemTemplate>
        </asp:TemplateField>
      </Columns>
    <FooterStyle BackColor="Silver" Height="25px" />
    <PagerStyle BackColor="DarkGray" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
</asp:GridView>



]]>

protected void chkSelect_CheckedChanged(object sender, EventArgs e)
       {
           if (chkSelect.Checked == true)
           {
               btnDisplay.text=chkSelect.Text;
           }
           else
           {
              btnDisplay.text="";
           }
       }


hi,
i need to get the value of the column which is a template field on check box and button click events of a gridview

am having gridview control, having 3 columns for row
1st column having checkboxes
2nd column string values
and 3rd column buttons
When i check the checkbox i want the value of 2nd column to be displayed on button click
can anyone suggest me the solution

thanks inadvance

解决方案

By using this code you have the gridview row in which the button was clicked.
in the onclick event of your button:

Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;



By using this code you have the gridview row in which the checkbox was clicked.
in the onselectedChanged event of your checkbox:

CheckBox chkbx = (CheckBox)sender;
GridViewRow row = (GridViewRow)chkbx.NamingContainer;
if (chkbx.Checked)
{
 /// your code here to do with the textbox what ever you need to do.

}



I cannot give a more precize answer cause I do not know whether the checkbox or the button shows the value in column 2


  <asp:GridView ID="grdFmlHist" runat="server" AllowPaging="True" Enabled ="False"
    AutoGenerateColumns="False" DataKeyNames="Relationship" Height="40px" WIDTH="450px"
    PageSize="8" ShowFooter="True"  onrowdatabound="grdFmlHist_RowDataBound" >
    <PagerSettings Mode="NumericFirstLast" />
    <RowStyle BackColor="WhiteSmoke" BorderColor="CornflowerBlue" ForeColor="Black"
        Height="30px" />
    <Columns>
    <asp:TemplateField HeaderText="S.No">
          <ItemTemplate>
              <asp:CheckBox ID="chkSelect" runat="server" />
        </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="S.No">
          <ItemTemplate>
            <%# ((GridViewRow)Container).RowIndex + 1%>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Relationship" HeaderText="Relationship" />
        <asp:BoundField DataField="Disease" HeaderText="Disease" />
        <asp:BoundField DataField="Dates" HeaderText="Dates" />
        <asp:TemplateField>
        <ItemTemplate>
            <asp:Button ID="btnDisplay" runat="server"  /></ItemTemplate>
        </asp:TemplateField>
      </Columns>
    <FooterStyle BackColor="Silver" Height="25px" />
    <PagerStyle BackColor="DarkGray" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
</asp:GridView>



]]>

protected void chkSelect_CheckedChanged(object sender, EventArgs e)
       {
           if (chkSelect.Checked == true)
           {
               btnDisplay.text=chkSelect.Text;
           }
           else
           {
              btnDisplay.text="";
           }
       }


这篇关于gridview复选框和按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆