如果标题没有数据绑定,单击“编辑"按钮时,如何在GridView行中获取TextBox? [英] How do I get TextBox in a GridView row when Edit button is clicked if the headers are not databound?

查看:53
本文介绍了如果标题没有数据绑定,单击“编辑"按钮时,如何在GridView行中获取TextBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView,并且GridView的标头字段是程序中ListBox中的项目.因此,每次运行时,生成的列数都是动态的.因此,基于此,如果我单击GridView内关联的编辑"按钮,如何为该行的每个单元格生成TextBox.

我编写的.cs代码是:

I have a GridView, and the header field of the GridView are the items from a ListBox in my program. Therefore the number of columns generated is dynamic every time I run. So based on this if I click on Edit button associated inside my GridView, how do I generate TextBox for each cell of that row.

The .cs code I have written is:

protected void DONE4_Click(object sender, EventArgs e)
    {
        Panel7.Visible = true;

        SqlDataAdapter mydat = new SqlDataAdapter("SELECT DISTINCT Profile_Instance FROM Profile_Master", con);
        DataSet dst = new DataSet();
        mydat.Fill(dst, "Table");
        ListBox3.Items.Clear();
        ListBox3.DataSource = dst.Tables[0];
        ListBox3.DataTextField = dst.Tables[0].Columns["Profile_Instance"].ColumnName;
        ListBox3.DataBind();

        int count = ListBox3.Items.Count;

        DataTable dt = new DataTable();
        DataRow rw = default(DataRow);
        for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            dt.Columns.Add(ListBox1.Items[i].ToString(),
                                           System.Type.GetType("System.String"));
        }

        for (int j = 0; j < count; j++)
        {
            rw = dt.NewRow();
            for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                rw[ListBox1.Items[i].ToString()] = " ";
            }
            dt.Rows.Add(rw);
        }
        GridView2.DataSource = dt;
        GridView2.DataBind();


        foreach (GridViewRow grdRow in GridView2.Rows)
        {
            DropDownList bind_dropdownlist = new DropDownList();                                                    // defining the property of the DropDownList as bind_dropdownlist
            bind_dropdownlist = (DropDownList)(GridView2.Rows[grdRow.RowIndex].Cells[0].FindControl("Pro_List"));   // finding the  DropDownList from the gridiew for binding
            SqlDataAdapter mydata = new SqlDataAdapter("SELECT DISTINCT Profile_Instance FROM Profile_Master", con);
            DataSet dset = new DataSet();                                                                           // binding the DropDownList with the dataset ds
            mydata.Fill(dset, "Table");
            bind_dropdownlist.DataSource = dset;
            bind_dropdownlist.DataTextField = "Profile_Instance";                                                   // set the DropDownList's DataTextField as designation which display the designation in the dropdownlist after fetching the data from database
            bind_dropdownlist.DataBind();
            bind_dropdownlist.Items.Insert(0, new ListItem("---Choose Profile---", "-1"));
        }
    }



GridView的设计代码为:



The design code for the GridView is:

<pre lang="HTML"><asp:Panel ID="Panel7" runat="server">
        <asp:GridView ID="GridView2" runat="server" CellPadding="4"
            style="text-align: center; font-size: small" BackColor="White" 
            BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px">

            <Columns>                
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:DropDownList ID="Pro_List" runat="server">
                        <asp:ListItem>--Select--</asp:ListItem>                          
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:CommandField ShowEditButton="True" />

            </Columns>
        </asp:GridView>
    </asp:Panel>



有人可以帮我吗?我希望问题清楚.



Can someone please help me on this? I hope the question is clear.

推荐答案

我认为您正在使用基于Ado.net连接的程序
尝试这样
i think you are using Ado.net connectivity based program
try like this
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional" >
   <ContentTemplate>
       <asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false"

        CssClass="Gridview"

        HeaderStyle-BackColor="#61A6F8" HeaderStyle-Font-Bold="true"

        HeaderStyle-ForeColor="White"

        ShowFooter="true" onpageindexchanging="OnPaging"

        PageSize="5" onrowcommand="gvDetails_RowCommand"

           onrowdatabound="gvDetails_RowDataBound"

           onrowcancelingedit="gvDetails_RowCancelingEdit"

           onrowediting="gvDetails_RowEditing">
        <Columns>
        <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="RecNo">
                <ItemTemplate>
                    <asp:Label ID="lblid" style="display:none" runat="server" Text='<%#Eval("Id")%>'/>
                    <asp:Label ID="lblRecNo" runat="server" Text='<%#Eval("RecNo")%>'/>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="lblEditId" style="display:none" runat="server" Text='<%#Eval("Id")%>'></asp:Label>
                <asp:Label ID="lblEditRecNo" width="100px"  runat="server" Text='<%#Eval("RecNo")%>' />
                </EditItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="TType">
                <ItemTemplate>
                    <asp:Label ID="lblTType" runat="server" Text='<%#Eval("TType")%>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList width="100px" ID="txtEditTType" runat="server"></asp:DropDownList>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="DropftrTType" width="100px" AutoPostBack="true" runat="server">
                    </asp:DropDownList>
                </FooterTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Op">
                <ItemTemplate>
                    <asp:Label ID="lblOp" runat="server" Text='<%#Eval("Op")%>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEditOp" CssClass="textBox" width="100px" Text='<%#Eval("Op")%>' runat="server"></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtftrOp" CssClass="textBox" Width="100px" onkeydown="return jsDecimals(event);" AutoPostBack="true"  runat="server" />
                </FooterTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Cl">
                <ItemTemplate>
                    <asp:Label ID="lblCl" runat="server" Text='<%#Eval("Cl")%>'/>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEditCl" CssClass="textBox" width="100px" Text='<%#Eval("Cl")%>' runat="server"></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtftrCl" CssClass="textBox" BackColor="InactiveCaption"  Width="100px"  onkeydown="return jsDecimals(event);" runat="server" />
                </FooterTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Date">
                <ItemTemplate>
                    <asp:Label ID="lblDate"  runat="server" Text='<%#Eval("Date")%>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEditDate" CssClass="textBox" width="80px" Text='<%#Eval("Date") %>' runat="server"></asp:TextBox>
                </EditItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Total">
                <ItemTemplate>
                    <asp:Label ID="lblTotal"  runat="server" Text='<%#Eval("Total")%>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEditTotal" CssClass="textBox" width="100px" Text='<%#Eval("Total")%>' runat="server"></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtftrTotal"  BackColor="InactiveCaption" CssClass="textBox" width="100px" runat="server" />
                </FooterTemplate>
                <HeaderStyle HorizontalAlign="Left" />
            </asp:TemplateField>

            <asp:TemplateField>
               <EditItemTemplate>
                <asp:ImageButton ID="imgbtnUpdate"  CommandName="DoAgain" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
                    <asp:ImageButton ID="imgbtnCancel" CommandName="Cancel" runat="server" ImageUrl="~/Images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
                </EditItemTemplate>
                <ItemTemplate>
                        <asp:ImageButton ID="imgbtnEdit" CommandName="Edit"  runat="server" ImageUrl="~/Images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" />
                        <asp:ImageButton ID="imgbtnDelete"  CommandName="DoItNow"   Text="Edit" runat="server" ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:ImageButton ID="imgbtnAdd" CommandName="Insert" runat="server" Height="30px" ImageUrl="~/Images/AddNewitem.jpg" ToolTip="Add new User" ValidationGroup="validaiton" Width="30px" />
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
        <HeaderStyle BackColor="#61A6F8" Font-Bold="True" ForeColor="White" />
    </asp:GridView>
   </ContentTemplate>
   </asp:UpdatePanel>


Code Behind file:


Code Behind file:

protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView drv = e.Row.DataItem as DataRowView;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList dp = (DropDownList)e.Row.FindControl("txtEditTType");
                DataTable dt = load_TicketTypes();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ListItem lt = new ListItem();
                    lt.Text = dt.Rows[i][0].ToString();
                    dp.Items.Add(lt);
                }
                dp.Items.Insert(0, new ListItem("Select", "Select"));
                string d=drv[2].ToString();
                dp.SelectedValue = d;
                //dp.Attributes.Add("onBlur", "Detect('"+dp.ClientID+"');");
            }

        }


问候
sarva


regards
sarva


hiii,
read this article will help you

http://msdn.microsoft.com/en-us/library/ms972948.aspx[^]
hiii,
read this article will help you

http://msdn.microsoft.com/en-us/library/ms972948.aspx[^]


这篇关于如果标题没有数据绑定,单击“编辑"按钮时,如何在GridView行中获取TextBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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