I have a question on C#. I use the GridView ItemTemplate to add a textbox to a whole column. I add the ID to the drop down list in ItemTemplate. Therefore, the generated ID of drop down list is 'GridViewID_dropdownListID_number' in each row when I launch the project.
However, I cannot set the drop down list to .Visible = true and .Visible = false in .aspx.cs file. I try to type the 'dropdownListID' and 'GridViewID_dropdownListID_number' to 'Page_Load' function. However, it displays the error message which is under light the statement.
'The name 'GridViewID_dropdownListID_0' does not exist in the current content'
Can I set the drop down list visible to true and false in .aspx.cs?
P.S I can retrieve the row number by GridViewRow
you can use FindControl
DropdownLIst tvSeries = (DropdownLIst)tableOfTVSeries.Rows[0].Cells[2].FindControl("tvSeriesTableCategoryDropdownLIst");
Here is an example how to do this in the item template of a repeater -- this is typically how this problem is solved:
<asp:DataList Runat="server" ...>
<ItemTemplate>
<asp:Label runat="Server" Text='<%# Container.DataItem("data") %>'
Visible='<%# Container.DataItem("makevisible") %>'/>
</ItemTemplate>
</asp:DataList>
Related
I have created a bulk edit gridview using Itemtemplate for each column. Label for non-edit mode, Textbox for edit mode. But this works only if the structure of gridview is known(Defining templates in asp).
<asp:TemplateField HeaderText="Name" ConvertEmptyStringToNull="True">
<ItemTemplate>
<asp:Label ID="lblName" Visible='<%# !(bool) IsInEditMode %>' runat="server" Text='<%# Eval("name") %>' />
<asp:TextBox ID="txtName" ControlStyle-CssClass="wide" Visible='<%# IsInEditMode %>'
runat="server" Text='<%# Eval("name") %>' />
</ItemTemplate>
</asp:TemplateField>
Now, what I am trying to achieve is when the user selects a dropdown value, the sql query is fired and it returns result. And this result is shown in gridview. But the problem arises because the number of columns in the result for each selected value from dropdown may vary. And i want to make the gridview as editable(or read-only). This requires two templates to be defined for each column.
So i want to know how this can be done dynamically i.e defining templates depending on number of columns returned by sql.
First off, you could trigger the query and following code with this event.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//insert code
}
For the issue of different templates, I might suggest using two GridViews. I've done this where I have two separate sets of results. If they choose a dropdownItem, it hides GridView1 and shows GridView2 (while querying GridView2.SqlDataSource).
Now are you querying different SQL Tables based on dropdown? Or are they querying from the same place?
I am using Grid View and inside grid view template defined for checkbox. I have used this criteria on various pages. But on some asp pages check box is not true as i have checked them it will always return property Checked = false.
<ItemTemplate>
<asp:CheckBox ID="chkBox_ID" runat="server" Checked='<%# Convert.ToBoolean( Convert.ToInt32(DataBinder.Eval(Container.DataItem, "COLUMN_NAME"))) %>' />
</ItemTemplate>
Template is defined above.
I am using foreach loop to get rows then cell and then FindControl() method to find the checkbox.
This is code behind how i access it.
CheckBox chkRow = (row.Cells[GetColumnIndexByName(Grv_h, "Authorize")].FindControl("chkBox_ID") as CheckBox);
Hello and thanks for taking your time to help me.
I'm trying to change the text of a textbox thats located inside my repeater.
<asp:Repeater runat="server" ID="rpCategories">
<HeaderTemplate>
<ul id="nav_down" class="nav_down">
</HeaderTemplate>
<ItemTemplate>
<li><%# Eval("Title") %></li>
</ItemTemplate>
<FooterTemplate>
<li></li>
<li>Contact</li>
<li><a id="cart_logo"></a>
<asp:Panel runat="server" ID="pnlBasket">
<asp:textbox runat="server" id="txtTotalCount" Enabled="false" CssClass="ltTotalCount"></asp:textbox>
</asp:Panel>
</li>
</ul>
</FooterTemplate>
</asp:Repeater>
It's the asp:textbox with the id="txtTotalCount" that I want to change the text of.
Here is my C# code:
TextBox ltTotalCount = (TextBox)FindControl("lblTotalCount");
ltTotalCount.Text = "1";
But if I run the code I get this error : Object reference not set to an instance of an object.
Would be so happy if someone could tell me what I'm doing wrong.
Becuase lblTotalCount is inside a parent control - the repeater, you have to reference it through the repeater.
You should be able to just add the id of your repeater before FindControl, like this...
TextBox ltTotalCount = (TextBox)rpCategories.FindControl("lblTotalCount");
You have to specify the repeater as the parent control to look for the text box and also since it's repeater its quite possible to have more than one text box with that Id so you have to specify which repeater item to look into like so:
TextBox ltTotalCount = rpCategories.Items[0].FindControl("txtTotalCount") as TextBox;
This will return the textbox in the first row of the repeater.
And you should use the Id value not the CssClass value
The ID is probably being 'enhanced' to prevent duplicate IDs in the rendered HTML. You can verify this by looking at the html.
You can add this to the textbox: ClientIDMode="Static" to make the ID stay the same.
You are getting the error because FindControl is returning a null but you are trying to access it anyway.
[Edit] Someone pointed out that this shouldn't work. I agree, here is what does work:
Control FooterTemplate = rpCategories.Controls[rpCategories.Controls.Count - 1].Controls[0];
TextBox MyTextBox = FooterTemplate.FindControl("txtTotalCount") as TextBox;
I have a DropDownList whose SelectedValue and DataSource are both databound. The control always selects the first item in the list regardless of the SelectedValue. The correct value is passed to the database when updating the value but the first item is always selected. What am I missing here?
<asp:DropDownList ID="SendAsDdl" runat="server"
SelectedValue='<%# Bind("SendAsId") %>' EnableViewState="true"
DataSource='<%# CM.Email.Users.GetSendAsList(OfficeId) %' />
You can't put scriplets into server side controls. You have to set the SelectedValue from your code behind:
SendAsDdl.SelectedValue = this.SendAsId;
I am using 2 linkbuttons inside seperate dataitem server controls on my asp.net web page
<asp:LinkButton ID="Item1" runat="server" CommandName="first"
OnCommand="Item1_Onclick" CommandArgument="<%# Container.DataItem %`>"
Text="<%# Container.DataItem %`>" >
</asp:LinkButton`>
and
<asp:LinkButton ID="Item2" runat="server" CommandName="second"
OnCommand="Item2_Onclick" CommandArgument="<%# Container.DataItem %`>"
Text="<%# Container.DataItem %`>" >
</asp:LinkButton`>
When I extract the command name inside c# as
e.CommandArgument.ToString().Trim();
it does give me the correct the name however the command arugument
e.CommandArgument.ToString().Trim();
for item2 is not what I expect. It is NOT that of item1, but the one that I set initially as datasource for the datalist control of item2. It does not give me the latest dataitem string value that I am expecting out of item2 linkbutton. What can be the problem? Where am I wrong?
Also, the event for item2 is fired ONLY for the first time and not after that? Is there some silly mistake that I am doing?
I got the problem. I had not included the if(!IsPostBack) as the first statement in my void Page_Load method !! That was stupid of me. Thanks anyways for all your time and ideas.
In the command argument you are not providing the property name in the data item
"<%# Container.DataItem.ProeprtyName %`>"
Say your datasource is a User object and you need the userid as the command argument it should be <%# Container.DataItem.UserID%>`