Dynamic population of usercontrol - c#

I have a custom usercontrol and it is a dropdownbox with a button beside it.
<asp:Panel ID="pnlSelect" runat="server">
<asp:Label ID="lblNameSelect" runat="server" Text="Name"></asp:Label>:
<asp:DropDownList ID="ddlDivision" runat="server"></asp:DropDownList>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</asp:Panel>
I need to create this during runtime.
UserControl.UCDropDownBox drpDivision = LoadControl("~/UserControl/UCDropDownBox.ascx") as UserControl.UCDropDownBox;
drpDivision.ID = "drp1";
drpDivision.LabelText = "Division";
drpDivision.DataSource = dt;
drpDivision.DataTextField = "colDescription";
drpDivision.DataValueField = "colValue";
phFormContent.Controls.Add(drpDivision);
Now I want to generate the next usercontrol (same usercontrol) when the button from the first usercontrol is clicked so I can get the selectedvalue from the first usercontrol.

Wouldn't this be a LOT easier if you put your UserControl in a Repeater?
<asp:Repeater ID="multiControls" runat="server">
<ItemTemplate>
<asp:Panel ID="pnlSelect" runat="server">
<asp:Label ID="lblNameSelect" runat="server" Text="Name"></asp:Label>:
<asp:DropDownList ID="ddlDivision" runat="server"></asp:DropDownList>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
You could then bind a collection of objects to the Repeater. Taking this approach, to solve your problem, all you need to do is:-
Add new item to collection
Re-bind Repeater
Not only does this vastly simplify the creation of new User Control objects, but it also makes loading previously persisted data a lot easier.

Related

findControl in Naming container (Asp.net webform C#)

I have a problem about findControl method in naming container.
It's not the first trouble about that and I would like understand the theory.
I found many solutions on website but nothing works
I have a DetailsView which contains controls.
I put DefaultMode "Insert" and I add 2 radio buttons
<asp:DetailsView ID="DetailsView1" runat="server"
ItemType="[...]"
DefaultMode="Insert"
[...]">
<Fields>
<asp:TemplateField>
<InsertItemTemplate>
<asp:Panel ID="Panel1" runat="server" GroupingText="Create or Select">
<div class="Select">
<asp:RadioButton ID="RB_Select" runat="server" Text="Select" Checked="True" AutoPostBack="true" OnCheckedChanged ="RB_Select_CheckedChanged" />
<asp:DropDownList runat="server" ID="DDL_Select"
ItemType="[...]"
[...]
AutoPostBack="true">
</asp:DropDownList>
</div>
<div class="New">
<asp:RadioButton ID="RB_New" runat="server" Text="New" Checked="false" AutoPostBack="true" OnCheckedChanged="RB_New_CheckedChanged" />
<asp:TextBox ID="TXB_New" runat="server" Enabled="false" Text="<%# BindItem.Label %>"></asp:TextBox>
</div>
</asp:Panel>
</InsertItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
And for exemple in my behind Code, I Just want to test if radiobutton is check or not :
protected void RB_New_CheckedChanged(object sender, EventArgs e)
{
var RadioButtonNew = (RadioButton)FindControl("RB_New");
var RadioButtonSelect = (RadioButton)FindControl("RB_Select");
RadioButtonSelect.Checked = !RadioButtonNew.Checked;
}
And I have a "System.NullReferenceException" because it doesn't find my controls.
Why it doesn't recognize my controls? And how to deal with this?
Thanks in advance
You are using FindControl on a Page level. But the Controls are inside a DetailsView, so you need to access that first.
TextBox tb = DetailsView1.FindControl("TXB_New") as TextBox;
//or
var RadioButtonSelect = (RadioButton)DetailsView1.FindControl("RB_Select");
Thank you again, I found the solution.
I didn't know but Panel element acted like a container.
I just add a findControl :
var RadioButtonSelect = (RadioButton)DetailsView1.FindControl("Panel1").FindControl("RB_Select");

C# Infinite Nested DataList

I have a relatively simple question about nested DataList.
How to dynamically nest infinite DataLists?
I didn't write any code yet, because I don't know how, but follow is an example:
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<br />
<asp:DataList ID="DataList2" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button2" runat="server" Text="Button" />
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
</form>
There are no problems when we have limited levels depth, we just nest two or three levels on DataList and we are done. But when we don't have a defined levels depth.
When the button clicked it loads the DataList2, when DataList2 button is clicked and it should load DataList3 and so far. And repeat it every time I click the button inside a DataList.
e.g.
-DatalistOUTER
--Some bindings
--Button [clicked]
[loads]
----DatalistINNER
-----Some bindings
-----Button [clicked]
[loads]
------DatalistINNERINNER
-------Somebindings
-------Button

Access Parent Page Control in Child User Control

I have an aspx page in which I have Ajax UpdatePanel which has a AJAX Tabcontainer which has 5 tab. In First tab I have a DropDownList for ProductID. In the second tab I have used a UserControl whose parameter needs to be reflected based on productID. I want to Access the DropDownList ProductID in the user control which I am not getting. Part of my code is
DropDownList IDList = (DropDownList)(this.Parent.FindControl("ProductID");
this is not working and I am getting NULL. I also have Tried
DropDownList IDList = (DropDownList)(this.Page.FindControl("ProductID");
Please tell me how I can do this.
As asked Part of necessary code is
<asp:UpdatePanel ID="UpdatePanelTankFormula" runat="server">
<ContentTemplate>
<asp:tabcontainer id="TabContainerTankFormula" AutoPostBack="true" runat="server" OnClientActiveTabChanged="clientActiveTabChanged" activetabindex="0" style="width:100%;">
<asp:TabPanel ID="InputDataTab" runat="server" HeaderText="Data Input">
<ContentTemplate>
<asp:DropDownList id="TankNameCombo" DataTextField="TankName" runat="server" AutoPostBack="true" DataValueField="customertankid" width="200px" onclick="javascript:SetHiddenField();"> </asp:DropDownList>
</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="LacticAcidAdditionTab" runat="server" HeaderText="Lactic Acid Addition">
<ContentTemplate>
//My user control
<UC:TankNote runat="server" ID="LaticTankNotesUC" GetNoteType="LAC" MEQMode="False"></UC:TankNote>
</ContentTemplate>
</asp:TabPanel>
</asp:tabcontainer>
<ContentTemplate>
</asp:UpdatePanel>
Now in the Code Behind of this User Control I want to access this DropDownList, which I am not getting. For the fix I have define a Public function that return the value of this list. But its a fix not solution.
You will have something like this in tab control.
<cc1:TabContainer ID="TabContainer1" runat="server">
<cc1:TabPanel ID="TabPanel1" runat="server">
<ContentTemplate>
<asp:DropDownList id="dropdownlist1" runat="Server"/>
</ContentTemplate>
</cc1:TabPanel>
So first you need to find tabPanel1 then find dropdownlist1 like following.
TabContainer TabContainer1= (TabContainer)(this.Page.FindControl("TabContainer1");
if (TabContainer1!=null){
TabPanel TabPanel1= (TabPanel)(this.TabContainer1.FindControl("TabPanel1");
if(TabPanel1 !=null){
DropDownList dropdownlist1= (DropDownList)(this.TabPanel1.FindControl("dropdownlist1");
}}

UpdatePanel stops CommandArgument from Updating

I have a button nested inside an update panel which has a CommandArgument tied to it. This calls a method which updates some label and text in an area not contained in the UpdatePanel. If I comment out the update panel the button works correctly so I know it is coming from the update panel. Anyone know how I can pass this through?
protected void Button_Command(object sender,
System.Web.UI.WebControls.CommandEventArgs e)
{
//update textboxes and labels here
}
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<asp:DataList ID="dListItems" runat="server" DataKeyField="PRODUCT_ID" RepeatColumns="4"
RepeatDirection="Horizontal" ShowFooter="False" ShowHeader="False" CellPadding="4">
<HeaderTemplate>
No Record Found....!
</HeaderTemplate>
<ItemTemplate>
<asp:Button ID="Button" runat="server" Text="Add to Cart"
CommandArgument='<%# Eval("Id") %>' CausesValidation="False"
CssClass="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
OnCommand="Button_Command"
/></span></span></p>
</ItemTemplate>
</asp:DataList>
</div>
</td>
</tr>
</table>
</div>
</ContentTemplate>
which updates some label and text in an area not contained in the
UpdatePanel
That's the problem. UpdatePanel will only update what is within and not what is outside. Try to put those control also the UpdatePanel and see them worked

How to make clicking on an <asp:Panel> (or a simple <div>) a trigger event for an UpdatePanel

I have an UpdatePanel in a Repeater.
There are a few CheckBoxes in the UpdatePanel with AutoPostBack="true"
There is a Label in the UpdatePanel. I set the Text value of the label in RepeaterName_ItemDataBound as it runs on every item generated.
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource" OnItemDataBound="R1_ItemDataBound">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<asp:Label ID="DateTimeLabel2" runat="server" Text="Label"></asp:Label>
<asp:Panel ID="panID" CssClass="actionicon_normal actionicon_compare" runat="server">
<%#XPath("ID")%>
<asp:CheckBox ID="chkID" runat="server" AutoPostBack="true" />
</asp:Panel>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:Repeater>
I want the CheckBoxes to automatically update the UpdatePanel as there were no repeater around, but possibly because the OnItemDataBound does not fire on every AsyncPostBack, nothing gets updated.
What is the proper way to do this?
In the onclick (JavaScript) call this function __doPostBack('idOfUpdatePanel', '');
Javascript and UpdatePanel

Categories