在网格视图中使用项目模板. [英] Using item templates in grid view.

查看:77
本文介绍了在网格视图中使用项目模板.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已通过网格视图中的项目模板添加了复选框列,并希望在文本框中查看所选行的值

Hi
I have added the checkbox column through item template in grid view and want to see the value of selected row in the text box

public partial class Default3 : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
       
        DataTable dt = new DataTable();
        //dt.Columns.Add(new DataColumn("select", typeof(bool)));
        dt.Columns.Add("Roll no");
        dt.Columns.Add("Name");
        dt.Columns.Add("marks");
        dt.Rows.Add("D001", "Aditya", "80");
        dt.Rows.Add("D002", "Arun", "75");
        dt.Rows.Add("Doo3", "Nikhil", "85");
        dt.Rows.Add("Doo3", "Nikhil", "85");
        dt.Rows.Add("Doo3", "Nikhil", "85");
        gvnew.DataSource = dt;
        gvnew.DataBind();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridViewRow row = gvnew.SelectedRow;
        row.Cells[4].Text = TextBox2.Text;  
        row.Cells[5].Text = TextBox3.Text;
    }
    protected void gvnew_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = gvnew.SelectedRow;
        TextBox1.Text= row.Cells[3].Text;
        TextBox2.Text= row.Cells[4].Text;
        TextBox3.Text = row.Cells[5].Text;
        TextBox1.ReadOnly = true;
           
    }

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in gvnew.Rows)
        {
            CheckBox chk = (CheckBox)row.FindControl("chkbselect");
            if (chk!=null && chk.Checked)
            {
                TextBox1.Text = gvnew.DataKeys[row.RowIndex].Value.ToString();
                TextBox2.Text = gvnew.Rows[0].Cells[0].Text.ToString();
                TextBox3.Text = row.Cells[5].Text;
                TextBox1.ReadOnly = true;
                //Response.Write("you have selected "+ gvnew.DataKeys[row.RowIndex].Value +"<br />");

            }
        
        }
    }
}

推荐答案

<asp:gridview id="gvnew" runat="server" cellpadding="4" forecolor="#333333" gridlines="None" onselectedindexchanged="gvnew_SelectedIndexChanged" xmlns:asp="#unknown"> <asp:templatefield>



///在这里使用itemtemplate



///use itemtemplate here

<itemtemplate>


 <asp:checkbox id="CheckBox1" runat="server" /> <asp:checkbox id="chkbselect" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" /> 

<pagerstyle backcolor="#2461BF" forecolor="White" horizontalalign="Center" /> <asp:checkbox id="Chkbselect" runat="server" checked="True" />


并使用C#代码


and in c# code

TextBox2.Text = gvnew.Rows[0].Cells[0].Text.ToString();// changed here
Textbox txt=(TextBox)row.findcontrol("textboxid");



而不是文本框只读,您可以在客户端使用

onkeypress="javascript: return false;"在文本框上.

试试吧


请投票对您有帮助



and rather than textbox readonly you can use on client side

onkeypress="javascript: return false;" on textbox.

try it


Please Vote if it helps you


这篇关于在网格视图中使用项目模板.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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