单击GridView行,然后信息将显示在asp.net中的C#中 [英] Click on GridView rows then info is displayed in C# in asp.net

查看:109
本文介绍了单击GridView行,然后信息将显示在asp.net中的C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击GridView1的第一行时,将显示相应的项.
同样,当我单击GridView1的第二行时,将显示相应的项目.
同样,当我单击GridView1的第三行(第一个单元格)时,将显示相应的项目.




我应该使用row_command方法吗?什么方法合适?

那么它的代码是什么.告诉我.

我正在使用此代码,但无法正常工作.


aspx代码是:---

When I click on first row of GridView1 then corresponding Items are displayed .
Similarly When I click on second row of GridView1 then corresponding items is displayed .
Similarly When I click on third row (first cell) of GridView1 then corresponding items is displayed .




Should I use row_command method? What method is suitable?

So what is the code for it. Tell me.

I m using this code but it is not working properly.


aspx code is:---

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

           CellPadding="4" ForeColor="#333333" onitemcommand="GridView1_RowCommand"

       Width="200px" onrowcommand="GridView1_RowCommand" >
           <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" />
       <Columns>
           <asp:TemplateField>
       <HeaderTemplate>
           Last Name
       </HeaderTemplate>

       <ItemTemplate>
           <asp:LinkButton ID="LinkButton7" runat="server" Text='<%# Eval("last") %>'

               Font-Underline="false" />
             </ItemTemplate>

       <EditItemTemplate>
           <asp:TextBox ID="TextBox6" runat="server" Text='<%#Bind("last") %>'></asp:TextBox>
       </EditItemTemplate>
       </asp:TemplateField>


          <asp:TemplateField>
       <HeaderTemplate>
           First Name
       </HeaderTemplate>
       <ItemTemplate>

           <asp:LinkButton ID="LinkButton8" runat="server" Text='<%# Eval("first") %>'

               Font-Underline="false" />
                </ItemTemplate>
       <EditItemTemplate>
           <asp:TextBox ID="TextBox7" runat="server" Text='<%#Bind("first") %>'></asp:TextBox>
       </EditItemTemplate>
       </asp:TemplateField>
       </Columns>
           <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
           <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
           <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
           <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
           <EditRowStyle BackColor="#999999" />
           <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
       </asp:GridView>




.cs代码为:-----

受保护的void GridView1_RowCommand(对象发送者,GridViewCommandEventArgs e)
{
试试
{
字符串selectSQL ="SELECT id,middle,company,title from contactmngt where id ="" + Session ["id"].ToString()+''";

SqlCommand cmd =新的SqlCommand(selectSQL,cnn);
SqlDataReader dr;
cnn.Open();
dr = cmd.ExecuteReader();
SqlDataAdapter da =新的SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.SelectCommand = cmd;
如果(dr.Read())
{
Label5.Text = dr [0] .ToString();
Label2.Text = dr [1] .ToString();
Label3.Text = dr [2] .ToString();
Label4.Text = dr [3] .ToString();

}
cnn.Close();
}

catch(ex ex例外)
{
Response.Write(ex.ToString());
}
}




The .cs code is:-----

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
string selectSQL = "SELECT id,middle,company,title FROM contactmngt where id=''" + Session["id"].ToString() + "''";

SqlCommand cmd = new SqlCommand(selectSQL, cnn);
SqlDataReader dr;
cnn.Open();
dr = cmd.ExecuteReader();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.SelectCommand = cmd;
if (dr.Read())
{
Label5.Text = dr[0].ToString();
Label2.Text = dr[1].ToString();
Label3.Text = dr[2].ToString();
Label4.Text = dr[3].ToString();

}
cnn.Close();
}

catch (Exception ex)
{
Response.Write(ex.ToString());
}
}

推荐答案



使用selectIndexChanged事件在选择行上显示结果

如果您使用rowcommand意味着必须有一个事件引发的控件,例如按钮或链接按钮....


最好的
Hi,

Use selectIndexChanged event for displaying results on select row

If you use rowcommand means there must be an event raised control like button or link button....


All the Best


Refer this link


http://www.dotnetcurry.com/ShowArticle.aspx?ID=212


这篇关于单击GridView行,然后信息将显示在asp.net中的C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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