GridView选择并显示在TextBox中 [英] GridView Selection and display in TextBox

查看:180
本文介绍了GridView选择并显示在TextBox中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何显示到文本框的网格视图行

How to display grid view row to text box

推荐答案

请参阅我的解决方案,它确实做到了
Please see my solution which exactly does that Link[^]


第一页:

设计页面:

First Page :

Design Page :

<div>
        <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("Id") %>'

                            CommandName="view">View</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>



后面的代码:




Code Behind :


protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    private void BindGrid()
    {
        using (SqlConnection con = new SqlConnection("Data Source=DELL-PC;Initial Catalog=Sample;Integrated Security=True"))
        {
            SqlCommand cmd = new SqlCommand("select * from tblUser", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
        if (e.CommandName == "view")
        {
            int Id = Convert.ToInt32(e.CommandArgument);
            Session["Id"] = Id;
            Response.Redirect("New.aspx");
        }
    }




添加另一个名称为New.aspx的页面,并遵循以下代码...

New.aspx

设计页面:





Add another page with name as New.aspx and follow the below code...

New.aspx

Design Page :


<div>
       <table class="style1">
           <tr>
               <td>
                   Id
               </td>
               <td style="margin-left: 40px">
                   <asp:TextBox ID="txtID" runat="server"></asp:TextBox>
               </td>
           </tr>
           <tr>
               <td>
                   First Name
               </td>
               <td>
                   <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
               </td>
           </tr>
           <tr>
               <td>
                   Last Name
               </td>
               <td>
                   <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
               </td>
           </tr>
       </table>
   </div>




New.aspx.cs




New.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int Id = Convert.ToInt32(Session["Id"]);
            using (SqlConnection con = new SqlConnection("Data Source=DELL-PC;Initial Catalog=Sample;Integrated Security=True"))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from tblUser where Id=" + Id + "", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    txtID.Text = Convert.ToString(dr["Id"]);
                    txtFirstName.Text = Convert.ToString(dr["FirstName"]);
                    txtLastName.Text = Convert.ToString(dr["LastName"]);
                }
                con.Close();
            }
        }
    }




SQL查询:




SQL Query :

CREATE TABLE [dbo].[tblUser](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [nvarchar](50) NULL,
    [LastName] [nvarchar](50) NULL,
 CONSTRAINT [PK_tblUser] PRIMARY KEY CLUSTERED
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


这篇关于GridView选择并显示在TextBox中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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