如何在asp.net中向gridviw添加动态控件 [英] How to adding dynamically controls to gridviw in asp.net
本文介绍了如何在asp.net中向gridviw添加动态控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对gridview有问题.在网格视图中,我有四个下拉列表(四个下拉列表在gridview中动态加载)和一个按钮.所以我的问题是,当我单击按钮时,它将显示第二行具有相同的控件,而上一行与我选择的相同,而下一行将允许我从下拉列表中选择数据,因此请单击按钮
请帮助我
问候
Ramesh
解决方案
看看这些入门指南:
GridView一目了然 [在GridView中就地编辑 [在ASP.NET 2.0中可编辑GridView [ ^ ]
带有文本框,复选框,单选按钮和下拉列表的可编辑Gridview [ ^ ]
< asp:GridView ID =" dvtransferload" runat 服务器" CellPadding =" 对于eColor =" span> true" =" 宽度 AutoGenerateColumns =" span> =" 124px" 样式 =" > < RowStyle =" #F7F6F3" ForeColor #333333 " / < FooterStyle =" #5D7B9D" 字体粗体 True" ForeColor =" 白色" / > < PagerStyle =" #284775" ForeColor 白色" HorizontalAlign =" 居中" / < SelectedRowStyle =" #E2DED6" 字体粗体 True" ForeColor =" #333333" / > < HeaderStyle =" #5D7B9D" 字体粗体 True" ForeColor =" 白色" / > < EditRowStyle =" #999999" / < AlternatingRowStyle =" 白色" ForeColor >#284775" / > < 列 > < asp:TemplateField HeaderText =" < FooterStyle =" 右" / < ItemTemplate > <%-DataSourceID = " DataTextField = " DataValueField = " dist_cd"-< asp:DropDownList ID =" runat 服务器" 宽度 140px" OnSelectedIndexChanged cmbdistName_SelectedIndexChanged" span> AutoPostBack =" 为真 > < /asp:DropDownList > < /ItemTemplate > < /asp:TemplateField > < asp:TemplateField HeaderText =" < ItemTemplate > < asp:DropDownList ID =" runat 服务器" 宽度 140px" OnSelectedIndexChanged cmbcircle_SelectedIndexChanged" span> AutoPostBack =" true" > < /asp:DropDownList > < /ItemTemplate > < /asp:TemplateField > < asp:TemplateField HeaderText =" < ItemTemplate > < asp:DropDownList ID =" runat 服务器" 宽度 140px" OnSelectedIndexChanged cmbArea1_SelectedIndexChanged" span> AppendDataBoundItems =" > < /asp:DropDownList > < /ItemTemplate > < FooterStyle =" 右" / < FooterTemplate > < asp:Button ID =" runat 服务器" 文本 提交" OnClick btnsubmit1_Click" Width =" / > < asp:Button ID =" runat 服务器" 文本 添加新行" OnClick =" > < /FooterTemplate > < /asp:TemplateField > <%-< asp:TemplateField HeaderText = " Juridiction" " SqlDataSource2" DataTextField = Cir_nm" DataValueField = " < ItemTemplate> DataSourceID = " DataTextField = " area_name" DataValueField = area_cd" < asp:DropDownList ID = " runat = " Width = " OnSelectedIndexChanged = " AppendDataBoundItems = " > </ asp:DropDownList > < /ItemTemplate > < /asp:TemplateField > -%> < /列 > < /asp:GridView >
受保护的 子 ButtonAdd_Click( ByVal 发件人 As 对象, ByVal e As EventArgs) Dim 计数 As 整数 For 计数= 0 要 dvtransferload.行.数数 如果 count = dvtransferload.Rows.Count- 1 >然后 dist_dropdownlist = CType (dvtransferload.Rows(count).Cells( 0 ).FindControl(" ),DropDownList) Circle_dropdownlist = CType (dvtransferload.Rows(count).Cells( 1 ).FindControl(" ),DropDownList) area_dropdownlist = CType (dvtransferload.Rows(count).Cells( 2 ).FindControl(" ),DropDownList) 如果 dist_dropdownlist.SelectedItem.Text<> " 然后 AddNewRow() 结束 如果 结束 如果 下一步 结束 子 受保护的 子 Page_Load( ByVal 发件人目标 对象, ByVal e 句柄 如果 不是 IsPostBack >然后 LoadRoleLevel() popCmbdistricLoad() SetInitialRow() 结束 如果 结束 子 受保护的 Sub SetInitialRow() Dim dt As New DataTable Dim dr As DataRow = 没什么 dt.Columns.Add(" , GetType (字符串)) dt.Columns.Add(" ,> GetType (字符串)) dt.Columns.Add(" , GetType (字符串)) dr = dt.NewRow() dr(" )= 字符串 .空的 dr(" )= 字符串 .空的 dr(" )= 字符串 .空的 dt.Rows.Add(dr) ViewState(" )= dt dvtransferload.DataSource = dt dvtransferload.DataBind() Dim drp As DropDownList = CType (dvtransferload .Rows( 0 ).Cells( 0 ).FindControl(" ),DropDownList) ' drp.Items.Add("Select") ' drp.SelectedValue ="Select" ' drp.SelectedItem.Text =选择" PopDistload() 结束 子 私有 Sub AddNewRow() Dim rowindex As 整数 = 0 如果 ViewState(" )是 什么都没有 然后 其他 Dim dtcurrenttable As DataTable = CType (ViewState (" ),数据表) Dim drCurrentRow As DataRow = 什么都没有 如果 dtcurrenttable.Rows.Count> 0 然后 对于 i As 整数 = 1 要 dtcurrenttable.Rows.Count dist_dropdownlist = CType (dvtransferload.Rows(rowindex).Cells( 0 ).FindControl(" ),DropDownList) Circle_dropdownlist = CType (dvtransferload.Rows(rowindex).Cells( 1 ).FindControl(" ),DropDownList) area_dropdownlist = CType (dvtransferload.Rows(rowindex).Cells( 2 ).FindControl(" ),DropDownList) p1 = dist_dropdownlist.SelectedValue p2 = Circle_dropdownlist.SelectedValue p3 = area_dropdownlist.SelectedValue 如果 dist_dropdownlist.SelectedItem.Text<> " 然后 drCurrentRow = dtcurrenttable.NewRow() dtcurrenttable.Rows(i- 1 )(" )= p1 dtcurrenttable.Rows(i- 1 )(" )= p2 dtcurrenttable.Rows(i- 1 )(" )= p3 rowindex = rowindex + 1 其他 结束 如果 下一个我 dtcurrenttable.Rows.Add(drCurrentRow) ViewState(" )= dtcurrenttable dvtransferload.DataSource = dtcurrenttable dvtransferload.DataBind() 其他 Response.Write(" ) 结束 如果 PopDistload() SetPreviousData() 结束 如果 结束 子 Dim 标志 As 布尔值 = 错误 私有 Sub SetPreviousData() Dim rowindex As 整数 = 0 如果 ViewState(" )是 什么都没有 然后 其他 Dim dt As DataTable = CType (ViewState (" ),数据表) 如果 dt.Rows.Count> 0 然后 对于 i As 整数 = 0 收件人 dt.Rows.Count- 1 ' 将FDCA_District更改为新的DFDCA_District Dim drp As DropDownList = CType (dvtransferload .Rows(rowindex).Cells( 0 ).FindControl(" ),DropDownList) Dim d As 整数 = drp.Items .IndexOf( CType (drp.Items.FindByValue((dt.Rows(i)(" DistrictCode").ToString())),ListItem)) 如果 d<> -1 然后 Dim dcd As String = dt.Rows (i)(" ).ToString() PopCircle1load(i,dcd) Dim cir As DropDownList = CType (dvtransferload .Rows(rowindex).Cells( 1 ).FindControl(" ),DropDownList) Dim c As 整数 = cir.Items .IndexOf( CType (cir.Items.FindByValue((dt.Rows(i)(" CircleCode").ToString())),ListItem)) Dim ccd As String = dt.Rows (i)(" ).ToString() PopAreaload(i,ccd) Dim 区域 As DropDownList = CType (dvtransferload .Rows(rowindex).Cells( 2 ).FindControl(" ),DropDownList) Dim 和 As 整数 = area. .IndexOf( CType (area.Items.FindByValue((dt.Rows(i)(" AreaCode").ToString())),ListItem)) drp.Items(d).Selected = 真 如果 c<> -1 然后 cir.Items(c).Selected = 真 如果是<> -1 然后 area.Items(a).Selected = 真 结束 如果 结束 如果 rowindex = rowindex + 1 其他 Dim drp1 As DropDownList = CType (dvtransferload .Rows(i).Cells( 0 ).FindControl(" ),DropDownList) 如果 drp1.Items.Contains( CType (drp1.Items.FindByText( 选择"),ListItem))= 然后 drp1.SelectedValue = " 其他 drp1.Items.Add(" ) drp1.SelectedValue = " drp1.SelectedItem.Text = " 结束 如果 结束 如果 下一步 结束 如果 结束 如果 结束 子
Sub PopDistload() Dim dt As DataTable = CType (ViewState (" ),数据表) Dim FDCA_District As 新建 DFDCA_District dvtransferload.DataSource = FDCA_District.Get_Districtmst() Dim grdRow As GridViewRow 对于 每个 grdRow 在 dvtransferload.Rows dist_dropdownlist = CType (((dvtransferload.Rows(grdRow.RowIndex).Cells( 0 ).FindControl(" )),DropDownList) dist_dropdownlist.DataSource = FDCA_District.Get_Districtmst() dist_dropdownlist.DataValueField = " dist_dropdownlist.DataTextField = " dist_dropdownlist.DataBind() dist_dropdownlist.Items.Insert(" ,新建 ListItem(" ,-1)) 下一步 结束 子
Sub PopCircle1load( ByVal ind As 整数, ByVal dcd As 字符串) Dim dt As DataTable = CType (ViewState (" ),数据表) Dim FDCA_District As 新建 DFDCA_District Dim strdist As 字符串 Dim rcount As 整数 = 0 Dim grdRow As GridViewRow 对于 每个 grdRow 在 dvtransferload.Rows 如果(rcount = ind)或(ind = -1 并且 dcd = " )然后 dist_dropdownlist = CType (((dvtransferload.Rows(grdRow.RowIndex).Cells( 0 ).FindControl(" )),DropDownList) Circle_dropdownlist = CType (((dvtransferload.Rows(grdRow.RowIndex).Cells( 1 ).FindControl(" )),DropDownList) 如果 dcd = " 然后 strdist = dist_dropdownlist.SelectedValue 其他 strdist = dcd 结束 如果 Circle_dropdownlist.DataSource = FDCA_District.Get_circlemaster_dist_cd(strdist) Circle_dropdownlist.DataTextField = " Circle_dropdownlist.DataValueField = " FDCA_District = 没什么 Circle_dropdownlist.DataBind() Circle_dropdownlist.Items.Insert(" ,新建 ListItem(" ,-1)) 结束 如果 rcount = rcount + 1 下一步 结束 子
Sub PopAreaload( ByVal ind As 整数, ByVal ccd As 字符串) Dim FDCA_District As 新建 DFDCA_District Dim strcir As 字符串 Dim rcount As 整数 = 0 Dim grdRow As GridViewRow 对于 每个 grdRow 在 dvtransferload.Rows 如果(rcount = ind)或(ind = -1 并且 ccd = " )然后 dist_dropdownlist = CType (((dvtransferload.Rows(grdRow.RowIndex).Cells( 0 ).FindControl(" )),DropDownList) Circle_dropdownlist = CType (((dvtransferload.Rows(grdRow.RowIndex).Cells( 1 ).FindControl(" )),DropDownList) area_dropdownlist = CType (((dvtransferload.Rows(grdRow.RowIndex).Cells( 2 ).FindControl(" )),DropDownList) 如果 ccd = " 然后 strcir = Circle_dropdownlist.SelectedValue 其他 strcir = ccd 结束 如果 area_dropdownlist.DataSource = 没什么 area_dropdownlist.DataSource = FDCA_District.Get_areamaster_dist_cd_circle_cd(strcir) area_dropdownlist.DataTextField = " area_dropdownlist.DataValueField = " FDCA_District = 没什么 area_dropdownlist.DataBind() area_dropdownlist.Items.Insert(" ,新建 ListItem(" ,-1)) 结束 如果 rcount = rcount + 1 下一步 结束 子
hi
i am Ramesh,
i have a problem with gridview. In grid view i have a four dropdownlist(four dropdownlist are loaded dynamically in gridview ) and one button. so my problem is when i click on button it will show the second row with same controls and previous row was same as i selected and next row will allow me to select data from the dropdownlist so on click on the button
please help me
Regards
Ramesh
解决方案
Have a look at these to get started:
GridView all in one[^]
Inplace Edit in GridView[^]
Editable GridView in ASP.NET 2.0[^]
Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List[^]
<asp:GridView ID="dvtransferload" runat="server" CellPadding="4" ForeColor="#333333" ShowFooter="true" GridLines="None" Width="594px" AutoGenerateColumns="False" Height="124px" style="margin-right: 0px"> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <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" /> <Columns> <asp:TemplateField HeaderText="District Name"> <FooterStyle HorizontalAlign="Right" /> <ItemTemplate><%--DataSourceID="SqlDataSource1" DataTextField="dist_name" DataValueField="dist_cd" --%> <asp:DropDownList ID="cmbdistName" runat="server" Width="140px" OnSelectedIndexChanged="cmbdistName_SelectedIndexChanged" AutoPostBack="true"> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Circle Name"> <ItemTemplate> <asp:DropDownList ID="cmbcircle" runat="server" Width="140px" OnSelectedIndexChanged="cmbcircle_SelectedIndexChanged" AutoPostBack="true" > </asp:DropDownList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Area"> <ItemTemplate> <asp:DropDownList ID="cmbArea1" runat="server" Width="140px" OnSelectedIndexChanged="cmbArea1_SelectedIndexChanged" AppendDataBoundItems="false"> </asp:DropDownList> </ItemTemplate> <FooterStyle HorizontalAlign="Right" /> <FooterTemplate> <asp:Button ID="btnsubmit1" runat="server" Text="Submit" OnClick="btnsubmit1_Click" Width="80px" /> <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" /> </FooterTemplate> </asp:TemplateField> <%--<asp:TemplateField HeaderText="Juridiction"> DataSourceID="SqlDataSource2" DataTextField="Cir_nm" DataValueField="cir_cd" <ItemTemplate> DataSourceID="SqlDataSource3" DataTextField="area_name" DataValueField="area_cd" <asp:DropDownList ID="cmbJuridiction" runat="server" Width="140px" OnSelectedIndexChanged="cmbJuridiction_SelectedIndexChanged" AppendDataBoundItems="true"> </asp:DropDownList> </ItemTemplate> </asp:TemplateField>--%> </Columns> </asp:GridView>
Protected Sub ButtonAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Dim count As Integer For count = 0 To dvtransferload.Rows.Count If count = dvtransferload.Rows.Count - 1 Then dist_dropdownlist = CType(dvtransferload.Rows(count).Cells(0).FindControl("cmbdistName"), DropDownList) Circle_dropdownlist = CType(dvtransferload.Rows(count).Cells(1).FindControl("cmbcircle"), DropDownList) area_dropdownlist = CType(dvtransferload.Rows(count).Cells(2).FindControl("cmbArea1"), DropDownList) If dist_dropdownlist.SelectedItem.Text <> "Select" Then AddNewRow() End If End If Next End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then LoadRoleLevel() popCmbdistricLoad() SetInitialRow() End If End Sub Protected Sub SetInitialRow() Dim dt As New DataTable Dim dr As DataRow = Nothing dt.Columns.Add("DistrictCode", GetType(String)) dt.Columns.Add("CircleCode", GetType(String)) dt.Columns.Add("AreaCode", GetType(String)) dr = dt.NewRow() dr("DistrictCode") = String.Empty dr("CircleCode") = String.Empty dr("AreaCode") = String.Empty dt.Rows.Add(dr) ViewState("CurrentTable") = dt dvtransferload.DataSource = dt dvtransferload.DataBind() Dim drp As DropDownList = CType(dvtransferload.Rows(0).Cells(0).FindControl("cmbdistName"), DropDownList) 'drp.Items.Add("Select") 'drp.SelectedValue = "Select" 'drp.SelectedItem.Text = "Select" PopDistload() End Sub Private Sub AddNewRow() Dim rowindex As Integer = 0 If ViewState("CurrentTable") Is Nothing Then Else Dim dtcurrenttable As DataTable = CType(ViewState("CurrentTable"), DataTable) Dim drCurrentRow As DataRow = Nothing If dtcurrenttable.Rows.Count > 0 Then For i As Integer = 1 To dtcurrenttable.Rows.Count dist_dropdownlist = CType(dvtransferload.Rows(rowindex).Cells(0).FindControl("cmbdistName"), DropDownList) Circle_dropdownlist = CType(dvtransferload.Rows(rowindex).Cells(1).FindControl("cmbcircle"), DropDownList) area_dropdownlist = CType(dvtransferload.Rows(rowindex).Cells(2).FindControl("cmbArea1"), DropDownList) p1 = dist_dropdownlist.SelectedValue p2 = Circle_dropdownlist.SelectedValue p3 = area_dropdownlist.SelectedValue If dist_dropdownlist.SelectedItem.Text <> "Select" Then drCurrentRow = dtcurrenttable.NewRow() dtcurrenttable.Rows(i - 1)("DistrictCode") = p1 dtcurrenttable.Rows(i - 1)("CircleCode") = p2 dtcurrenttable.Rows(i - 1)("AreaCode") = p3 rowindex = rowindex + 1 Else End If Next i dtcurrenttable.Rows.Add(drCurrentRow) ViewState("CurrentTable") = dtcurrenttable dvtransferload.DataSource = dtcurrenttable dvtransferload.DataBind() Else Response.Write("error") End If PopDistload() SetPreviousData() End If End Sub Dim flag As Boolean = False Private Sub SetPreviousData() Dim rowindex As Integer = 0 If ViewState("CurrentTable") Is Nothing Then Else Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable) If dt.Rows.Count > 0 Then For i As Integer = 0 To dt.Rows.Count - 1 'Dim FDCA_District As New DFDCA_District Dim drp As DropDownList = CType(dvtransferload.Rows(rowindex).Cells(0).FindControl("cmbdistName"), DropDownList) Dim d As Integer = drp.Items.IndexOf(CType(drp.Items.FindByValue((dt.Rows(i)("DistrictCode").ToString())), ListItem)) If d <> -1 Then Dim dcd As String = dt.Rows(i)("DistrictCode").ToString() PopCircle1load(i, dcd) Dim cir As DropDownList = CType(dvtransferload.Rows(rowindex).Cells(1).FindControl("cmbcircle"), DropDownList) Dim c As Integer = cir.Items.IndexOf(CType(cir.Items.FindByValue((dt.Rows(i)("CircleCode").ToString())), ListItem)) Dim ccd As String = dt.Rows(i)("CircleCode").ToString() PopAreaload(i, ccd) Dim area As DropDownList = CType(dvtransferload.Rows(rowindex).Cells(2).FindControl("cmbArea1"), DropDownList) Dim a As Integer = area.Items.IndexOf(CType(area.Items.FindByValue((dt.Rows(i)("AreaCode").ToString())), ListItem)) drp.Items(d).Selected = True If c <> -1 Then cir.Items(c).Selected = True If a <> -1 Then area.Items(a).Selected = True End If End If rowindex = rowindex + 1 Else Dim drp1 As DropDownList = CType(dvtransferload.Rows(i).Cells(0).FindControl("cmbdistName"), DropDownList) If drp1.Items.Contains(CType(drp1.Items.FindByText("Select"), ListItem)) = True Then drp1.SelectedValue = "0" Else drp1.Items.Add("Select") drp1.SelectedValue = "0" drp1.SelectedItem.Text = "Select" End If End If Next End If End If End Sub
Sub PopDistload() Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable) Dim FDCA_District As New DFDCA_District dvtransferload.DataSource = FDCA_District.Get_Districtmst() Dim grdRow As GridViewRow For Each grdRow In dvtransferload.Rows dist_dropdownlist = CType((dvtransferload.Rows(grdRow.RowIndex).Cells(0).FindControl("cmbdistName")), DropDownList) dist_dropdownlist.DataSource = FDCA_District.Get_Districtmst() dist_dropdownlist.DataValueField = "dist_cd" dist_dropdownlist.DataTextField = "dist_name" dist_dropdownlist.DataBind() dist_dropdownlist.Items.Insert("0", New ListItem("----Select----", -1)) Next End Sub
Sub PopCircle1load(ByVal ind As Integer, ByVal dcd As String) Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable) Dim FDCA_District As New DFDCA_District Dim strdist As String Dim rcount As Integer = 0 Dim grdRow As GridViewRow For Each grdRow In dvtransferload.Rows If (rcount = ind) Or (ind = -1 And dcd = "") Then dist_dropdownlist = CType((dvtransferload.Rows(grdRow.RowIndex).Cells(0).FindControl("cmbdistName")), DropDownList) Circle_dropdownlist = CType((dvtransferload.Rows(grdRow.RowIndex).Cells(1).FindControl("cmbcircle")), DropDownList) If dcd = "" Then strdist = dist_dropdownlist.SelectedValue Else strdist = dcd End If Circle_dropdownlist.DataSource = FDCA_District.Get_circlemaster_dist_cd(strdist) Circle_dropdownlist.DataTextField = "Cir_nm" Circle_dropdownlist.DataValueField = "Cir_cd" FDCA_District = Nothing Circle_dropdownlist.DataBind() Circle_dropdownlist.Items.Insert("0", New ListItem("----Select----", -1)) End If rcount = rcount + 1 Next End Sub
Sub PopAreaload(ByVal ind As Integer, ByVal ccd As String) Dim FDCA_District As New DFDCA_District Dim strcir As String Dim rcount As Integer = 0 Dim grdRow As GridViewRow For Each grdRow In dvtransferload.Rows If (rcount = ind) Or (ind = -1 And ccd = "") Then dist_dropdownlist = CType((dvtransferload.Rows(grdRow.RowIndex).Cells(0).FindControl("cmbdistName")), DropDownList) Circle_dropdownlist = CType((dvtransferload.Rows(grdRow.RowIndex).Cells(1).FindControl("cmbcircle")), DropDownList) area_dropdownlist = CType((dvtransferload.Rows(grdRow.RowIndex).Cells(2).FindControl("cmbArea1")), DropDownList) If ccd = "" Then strcir = Circle_dropdownlist.SelectedValue Else strcir = ccd End If area_dropdownlist.DataSource = Nothing area_dropdownlist.DataSource = FDCA_District.Get_areamaster_dist_cd_circle_cd(strcir) area_dropdownlist.DataTextField = "area_name" area_dropdownlist.DataValueField = "area_cd" FDCA_District = Nothing area_dropdownlist.DataBind() area_dropdownlist.Items.Insert("0", New ListItem("----Select----", -1)) End If rcount = rcount + 1 Next End Sub
这篇关于如何在asp.net中向gridviw添加动态控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文