在网格视图中动态添加控件并检索用户在Asp.net中输入的值
本文关键字:Asp net 输入 用户 视图 网格 动态 添加 控件 检索 | 更新日期: 2023-09-27 18:17:52
我使用Asp。Net Gridview里面有2列。第一列有一个下拉菜单,根据选择,我在第二列内动态添加一个控件,就像它可能是TextBox
, Dropdown
等。这工作得很好。现在,如果我想从用户输入的TextBox
中检索值,我该怎么做?
我的代码如下:ASPX页面:
<asp:GridView ID="CRFormGridView" runat="server" AutoGenerateColumns="false"
OnRowDataBound="CRFormGridView_RowDataBound" AllowPaging="true" PageSize="25" ClientIDMode="Static" OnDataBound="CRFormGridView_DataBound">
<Columns>
<asp:BoundField DataField="CRListID" HeaderText="CR List ID" Visible="false" />
<asp:TemplateField HeaderText="Change Type">
<ItemTemplate>
<asp:UpdatePanel ID="updtpnlChangeType" runat="server" ClientIDMode="Static" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList Width="150" runat="server" ID="ddlChangeType" ClientIDMode="Static" AutoPostBack="true" OnSelectedIndexChanged="ddlChangeType_SelectedIndexChanged">
<asp:ListItem Text="" Value="0"></asp:ListItem>
<asp:ListItem Text="Update Offer" Value="1"></asp:ListItem>
<asp:ListItem Text="Add Component" Value="2"></asp:ListItem>
<asp:ListItem Text="Cancel Component" Value="3"></asp:ListItem>
<asp:ListItem Text="Update Request" Value="4"></asp:ListItem>
<asp:ListItem Text="Add Request" Value="5"></asp:ListItem>
<asp:ListItem Text="Cancel Request" Value="6"></asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Change Sub Type">
<ItemTemplate>
<asp:UpdatePanel ID="updtpnlChangeSubType" runat="server" ClientIDMode="Static" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList Width="150" runat="server" ID="ddlChangeSubType" ClientIDMode="Static" AutoPostBack="true" OnSelectedIndexChanged="ddlChangeSubType_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Request/Comments">
<ItemTemplate>
<asp:UpdatePanel ID="updtpnlDynamicControl" runat="server" ClientIDMode="Static" UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder ID="placehldrDynamicCnrtl" runat="server" ClientIDMode="Static" OnPreRender="placehldrDynamicCnrtl_PreRender"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Cs Page Code:
protected void ddlChangeType_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlChangeType = (DropDownList)sender;
GridViewRow currentRow = (GridViewRow)ddlChangeType.NamingContainer;
DropDownList ddlChangeSubType = currentRow.FindControl("ddlChangeSubType") as DropDownList;
object objControl;
if (ddlChangeSubType != null && currentRow != null && ddlChangeType != null)
{
ddlChangeSubType.DataTextField = "Desc";
ddlChangeSubType.DataValueField = "ID";
ddlChangeSubType.DataSource = setChangeSubType(ddlChangeType.SelectedValue);
ddlChangeSubType.DataBind();
if (Session["DynamicControls"] != null)
{
for (int y = 0; y < CRFormGridView.Rows.Count; y++)
{
if (((Dictionary<int, object>)Session["DynamicControls"]).TryGetValue(y, out objControl))
{
if (currentRow.RowIndex == y)
objControlsDict.Remove(y);
else
objControlsDict.Add(y, objControl);
}
}
}
Session.Add("DynamicControls", objControlsDict);
}
}
protected void ddlChangeSubType_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlChangeSubType = (DropDownList)sender;
GridViewRow currentRow = (GridViewRow)ddlChangeSubType.NamingContainer;
DropDownList ddlChangeType = currentRow.FindControl("ddlChangeType") as DropDownList;
//TextBox txt = currentRow.FindControl("txt") as TextBox;
PlaceHolder placehldr = currentRow.FindControl("placehldrDynamicCnrtl") as PlaceHolder;
object objControl;
rowIndex = currentRow.RowIndex;
if (Session["DynamicControls"] != null)
{
for (int y = 0; y < CRFormGridView.Rows.Count; y++)
{
if (((Dictionary<int, object>)Session["DynamicControls"]).TryGetValue(y, out objControl))
{
objControlsDict.Add(y, objControl);
}
}
}
if (ddlChangeSubType != null && currentRow != null && ddlChangeSubType != null)
{
switch (ddlChangeType.SelectedItem.Text.ToUpper())
{
case "UPDATE OFFER":
TextBox txtBox = new TextBox();
txtBox.Text = "Text Box Added";
txtBox.ID = "txt";
txtBox.ClientIDMode = ClientIDMode.Static;
txtBox.EnableViewState = true;
placehldr.Controls.Add(txtBox);
if (objControlsDict.ContainsKey(rowIndex))
objControlsDict.Remove(rowIndex);
objControlsDict.Add(rowIndex, txtBox);
break;
case "ADD COMPONENT":
Label lbl = new Label();
lbl.Text = "Label Added";
lbl.ID = "lbl";
lbl.ClientIDMode = ClientIDMode.Static;
lbl.EnableViewState = true;
placehldr.Controls.Add(lbl);
if (objControlsDict.ContainsKey(rowIndex))
objControlsDict.Remove(rowIndex);
objControlsDict.Add(rowIndex, lbl);
break;
case "UPDATE REQUEST":
break;
default:
break;
}
Session.Add("DynamicControls", objControlsDict);
}
}
protected void placehldrDynamicCnrtl_PreRender(object sender, EventArgs e)
{
try
{
if (Page.IsPostBack)
{
PlaceHolder placeHldr = (PlaceHolder)sender;
GridViewRow currentRow = (GridViewRow)placeHldr.NamingContainer;
objControlsDict = (Dictionary<int, object>)Session["DynamicControls"];
if (objControlsDict != null)
{
if (objControlsDict.ContainsKey(count) && objControlsDict[count] is TextBox)
{
TextBox txtBox = (TextBox)objControlsDict[count];
txtBox.Text = "Text Box Added";
txtBox.ID = "txt";
txtBox.ClientIDMode = ClientIDMode.Static;
txtBox.EnableViewState = true;
((PlaceHolder)this.CRFormGridView.Rows[count].Cells[3].FindControl(
"placehldrDynamicCnrtl")).Controls.Add(txtBox);
}
if (objControlsDict.ContainsKey(count) && objControlsDict[count] is Label)
{
Label lbl = (Label)objControlsDict[count];
lbl.Text = "Label Added";
lbl.ID = "lbl";
lbl.ClientIDMode = ClientIDMode.Static;
lbl.EnableViewState = true;
((PlaceHolder)this.CRFormGridView.Rows[count].Cells[3].FindControl(
"placehldrDynamicCnrtl")).Controls.Add(lbl);
}
count++;
}
}
}
catch (Exception es)
{
throw;
}
}