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");
Related
I have the following situation:
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:ImageButton OnClick="imgSearchResult_Click" BackColor="#333333" ID="imgSearchResult" height="32" width="32" runat="server" ToolTip='<%# Eval("ToolTip") %>' ImageUrl='<%# Eval("ImageUrl") %>'/>
<asp:Label ID="labValue" Text='<%# Eval("Text") %>' runat="server" Width="32" />
</ItemTemplate>
</asp:Repeater>
The Repeater contains ImageButton and Label objects. If I only use one object, I can add them from codebehind through the binding:
Repeater.DataSource = imageList;
Repeater.DataBind();
OR
Repeater.DataSource = labelList;
Repeater.DataBind();
How can I add both to one Repeater?
If you need them in one Repeater, they -are- related in that "view" (loosely used term) of the data. So why not make that view into an explicit object that contains both the image and the label? Either named, or anonymous:
return Enumerable.Zip(imageList, labellist, (image, label) => new {image, label})
Alternatively, you can just pass the images and query for the labels by index:
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:Label ID="labValue" Text='<%# GetLabelText(Container.ItemIndex) %>' runat="server" Width="32" />
</ItemTemplate>
</asp:Repeater>
This requires a public GetLabelText(index) method and probably extra state in your code behind to make the ImageList available to that method. Not very nice, but it gets the job done.
I need to use DataList inside another DataList. It works fine for me, but when I'm trying to do something in code behind with this inside one it just doesn't exist for C#. Here is code:
...
<asp:DataList ID="DataListDziennik" runat="server"
DataSourceID="SqlDataSourcePrzedmioty">
<ItemTemplate>
<asp:Label ID="LabelPrzedmiot" runat="server" Text='<%# Eval("przedmiot") %>' />
...
<asp:DataList ID="DataListOceny" runat="server"
DataSourceID="SqlDataSourceOceny"
RepeatDirection="Horizontal"
OnItemCommand="DataListOceny_ItemCommandOceny"
OnEditCommand="DataListOceny_EditCommandOceny">
<EditItemTemplate>
<asp:TextBox ID="TextBoxOcena" runat="server" Text='<%# Bind("lista") %>' />
<td><asp:Button ID="ButtonZapisz" CommandName="Update" runat="server" Text="Zapisz" /></td>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox Width="20" ID="TextBoxOcena" ReadOnly="true" Text='<%# Eval("lista") %>' runat="server"></asp:TextBox>
<td><asp:Button ID="ButtonEdytuj" CommandName="Edit" runat="server" Text="Edytuj" /></td>
</ItemTemplate>
</asp:DataList>
</td>
</ItemTemplate>
</asp:DataList>
When I write this in code behind:
protected void DataListOceny_EditCommand(object source, DataListCommandEventArgs e)
{
DataListOceny.EditItemIndex = e.Item.ItemIndex;
DataListOceny.DataBind();
}
...Visual Studio tells me that DataListOceny does not exist in current content. I just want to be able edit items on DataListOceny after clicking the "edit" button, it can be placed anywhere on website. Do you know any solution for this problem?
Because DataListOceny is a control inside of another control, you have to make a reference to it by doing something like:
DataList DataListOceny = (DataList)e.Item.FindControl("DataListOceny");
Once you do that, you can use the DataListOceny variable. Hope this helps.
I've got something like that
<asp:ListView ID="lv" runat="server">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" />
<label id="test" runat="server">dw</label>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
Can someone tell me, how should I change label text, using C# code?
Main problem for me is- how to get access to the nested control (label, literal) from c# code?
EDITED:
<SelectedItemTemplate>
<asp:HiddenField ID="NumberEdit" runat="server"
Value='<%# Bind("numbers") %>' />
<label for="NameEdit">Name:</label>
<asp:TextBox ID="NameEdit" Width="160px" runat="server" AutoPostBack="true" OnTextChanged="NameEdit_TextChanged"
Text='<%# Bind("Name") %>' />
<br />
<label for="ShortcutEdit">Shortcut:</label>
<asp:TextBox ID="ShortcutEdit" Width="80px" runat="server"
Text='<%# Bind("Shortcut") %>' />
<br />
and I would like to generate automatically Shortcut text when user will change Name (Shortcut = 2 first letters from NameEdit)? Can you explain me, how should I do it? –
You would want to have an ItemDataBound event handler to get access to the controls for that particular item in your listview. The example on the page I linked should help you out.
First thing is that you need a data source binded with this ListView control, for example SqlDataSource, or any other allowed type you need:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:YourConnectionString %>"
SelectCommand="SELECT [Title], [id] FROM [Articles]"></asp:SqlDataSource>
<asp:ListView ID="lv" runat="server" DataSourceID="SqlDataSource1" >
// rest of the code
</asp:ListView>
Second thing is that controls from LayoutTemplate template will be rendered only if there is any data to show. So if you have datasource, but it is empty, this tamplate will not be applied. But you can use EmptyDataTemplate to display info when there is nothing from the datasource to display.
And then, when you already have datasource defined and binded to your ListView and there is data that will be displayed, the LayoutTemplate will be rendered. And then you can use FindControl method of the ListView. As for example of getting this literal:
Literal l = (Literal)lv.FindControl("litControlTitle");
It's returning null for you because you have no data to display, so controls are not rendered at all.
((Label)ListView1.FindControl("test")).Text = "Hello!";
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
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.