Using dropdown names as variable in asp.net - c#

I wants to use dropdown names as variable in asp.net webpage.
We have 10 dropdownlist with id- ddl1,ddl2,....ddl10.
What my requirement is that i need to show and hide from the code behind file
<div id ="divContainer" class="field" runat="server">
<asp:Label ID="lblmsg1" runat="server" Text="" CssClass="fieldtxt">
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="fieldtxtbox1" OnSelectedIndexChanged="ddlCommand_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" CssClass="fieldtxtbox1" OnSelectedIndexChanged="ddlCommand_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" CssClass="fieldtxtbox1" OnSelectedIndexChanged="ddlCommand_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList4" runat="server" CssClass="fieldtxtbox1" OnSelectedIndexChanged="ddlCommand_SelectedIndexChanged">
</asp:DropDownList>
//like this 10 different DropDownList
</div>
i have certain loop for doing that.
i need something like this...
ddl[i].Visible = true;
when i tried this got error like
'System.Web.UI.WebControls.DropDownList' is a 'type' but is used like a 'variable'
Please help

This should do the trick
DropDownList ddls = null;
for (int i = 1; i <= 10; i++) {
ddls = (DropDownList)this.FindControl("DropDownList" + i);
if (ddls != null) {
ddls.Visible = true;
}
}
The controls should be immediate children of the page. If these are embedded in other server control such as FormView, GridView etc or on Master page, then you must use that control or master in conjunction with FindControl

Why not using javascript?
//save the div in a var
//check the name of the div in the generated html, it might be different from the name you have in asp.net
var divContainer = document.getElementById("divContainer");
//get all ddl contsols by Tag (tag for ddl is select)
var ddls = divContainer.GetElementByTag("select");
//iterate through the found select controls from the div and set them to visible
for (var i = 0, len = ddls.length; i < len; ++i)
{
ddls[i].style.display='block';
}

Related

label of selected row in gridview asp.net with jquery

I would like to return text label in my gridview with Jquery
<div class="risksContainer">
<div class="risksGrid">
<asp:GridView ID="GridViewRisks" runat="server"
[...]
/>
<Columns>
<asp:TemplateField HeaderText="...">
<ItemTemplate>
<div class="CodeProductColumn">
<asp:Label ID="IDRisk" runat="server" CssClass="IDRiskIndex" Text="<%# Item.ID_Risk %>" />
<asp:Repeater ID="LabelRepeatCodeProduct" runat="server"
[....]
</asp:Repeater>
</div>
</ItemTemplate>
I try many solutions, but i don't know how i get the label Text of my selected row.
javascript :
function blabla(){
var Id_risk = $(".risksContainer .risksGrid .IDRiskIndex").text();
alert("Id_risk =" + Id_risk );
return Id_risk ;
}
(this function start when i click on edit button in my row)
I get text of all rows in my gridview and not only my selected row.
I try with "parent, child, first, selected, rows[]...
I am beginner and i'm desperate to find it
Use try in JavaScript:
<script type="text/javascript">
function blabla()
{
// find gridview
var gv = document.getElementById("<%=GridViewRisks.ID %>");
// get row count
var gvRowCount = gv.rows.length;
var i = 1;
for (i; i<= gvRowCount - 1; i++)
{
// get label value from column 2
alert(gv.rows[i].cells[2].childNodes[1].innerHTML);
}
return false;
}
</script>
<asp:Button ID="Button1" runat="server" OnClientClick=" blabla();return false;" ... />
Assuming you set the selected row in code behind with GridViewRisks.SelectedIndex = i, you can set the CSS class of the selected row.
<asp:GridView ID="GridViewRisks" runat="server" SelectedRowStyle-CssClass="selectedRow">
Now you can use that class to get the correct row in jQuery.
var Id_risk = $(".selectedRow .IDRiskIndex").text();

How to use Repeater inside RadioButtonList to repeat ListItem of RadioButtonList

First, Can repeater with in a repeater be used?
If yes than how I can use nested repeater in following scenario.
<div class="row">
<asp:Repeater ID="rp_Question" runat="server">
<ItemTemplate>
<p class="_100">
<h2 id="h4_Question" runat="server"><%# Eval("question_text") %></h2>
</p>
<p class="left">
<asp:RadioButtonList ID="rb_Question" runat="server">
<asp:ListItem Text="Option1" Value="1"></asp:ListItem>
<asp:ListItem Text="Option2" Value="2"></asp:ListItem>
<asp:ListItem Text="Option3" Value="3"></asp:ListItem>
<asp:ListItem Text="Option4" Value="4"></asp:ListItem>
</asp:RadioButtonList>
</p>
</ItemTemplate>
</asp:Repeater
Repeater Binding
rp_Question.DataSource = _question.GetAll();
rp_Question.DataBind();
The options of each question are saved in database, minimum option could be 3 and maximum could be 6. How can I use an other repeater inside rp_Question to repeat options of each question.
I want to show out put like this.
Expanding on the answer KateCute gave, you can use the ItemDataBound event for that.
<asp:Repeater ID="rp_Question" runat="server" OnItemDataBound="rp_Question_ItemDataBound">
And then in code behind.
protected void rp_Question_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//find the radiobuttonlist with findcontrol and cast back to it's original type
RadioButtonList rb_Question = e.Item.FindControl("rb_Question") as RadioButtonList;
//get the current datarow
DataRowView row = e.Item.DataItem as DataRowView;
//get the id from the datarow object
string questionID = row["question_id"].ToString();
//get the answers from the db with questionID and bind them as listitems just like in the loop below
//just a loop to add some listitems for demo
for (int i = 0; i < 5; i++)
{
rb_Question.Items.Insert(i, new ListItem("Option " + i.ToString(), i.ToString(), true));
}
}
Unfortunately, you can't use repeater inside asp:RadioButtonList. It allows only ListItem inside. You will get an error, that repeater is a not known element. But you can bind asp:RadioButtonList in code behind.

Gridview rowdatabound error : Multiple controls with the same ID 'hlLink' were found. FindControl requires that controls have unique ID

I have a gridview that has link and description to be rendered on the page.
written the below code in gridview in .aspx
<Columns>
<asp:TemplateField>
<ItemTemplate>
<p>
<asp:HyperLink ID="hlLink" runat="server" Target="_self"></asp:HyperLink></p>
</ItemTemplate>
<ItemTemplate>
<p>
<asp:Literal ID="litSummary" runat="server"></asp:Literal></p>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<AlternatingItemTemplate>
<p>
<asp:HyperLink ID="hlLink" runat="server" Target="_self"></asp:HyperLink></p>
</AlternatingItemTemplate>
<AlternatingItemTemplate>
<p>
<asp:Literal ID="litSummary" runat="server"></asp:Literal></p>
</AlternatingItemTemplate>
</asp:TemplateField>
</Columns>
and below in .aspx.csin gridview rowdataboundevent
protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SearchResultItem data = (SearchResultItem)e.Row.DataItem;
HyperLink hlLink = (HyperLink)e.Row.FindControl("hlLink");
Literal litSummary = (Literal)e.Row.FindControl("litSummary");
if (data.Description != null)
{
hlLink.Text = data.Title;
hlLink.NavigateUrl = data.Path.Replace("&", "&");
litSummary.Text = data.Description;
}
else
{
hlLink.Text = data.Path;
hlLink.NavigateUrl = data.Path.Replace("&", "&");
litSummary.Text = data.Path;
}
}
here SearchResultItem: is the result item that has link and description details.
First time when row bound event is called, it binds the data correctly, second time when called throws error "Multiple controls with the same ID 'hlLink' were found. FindControl requires that controls have unique IDs.
Please let me know whats error with the code.
Thanks
Problem : you are trying to create the same controls with same ID multiple times.
Solution : you need to remove the controls before creating them.
Try This:
void RemoveControls()
{
HyperLink l1 = (HyperLink)Page.FindControl("hlLink");
Literal l2 = (Literal)Page.FindControl("litSummary");
if(l1!= null)
Page.Controls.Remove(l1);
if(l2!= null)
Page.Controls.Remove(l2);
}
Solution 2: Pagination for Repeater control.
for implementing pagination in Repeater control you need to create PagedDataSource.
Try This:
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 8;//page sizes

Select All Checkbox feature in gridview not being implemented

I am having header in gridview that labels as "xls" and a checkbox, that when selected should select all the checkbox columns in gridview and unchecking the xls column should uncheck all the columns.
I am following two links:
Link-1
Here, the totalChkBoxes variable is coming null (despite my gridview has rows). In fact when debugging the JS, code inside parseInt and below line is coming as ''
Link-2
Here also the GridView2 variable is coming null.
One common change that i am doing in both the JS is replacing the <%=.....%> by <%#....%>
Please guide as to what i am doing wrong. You can also help by giving some suitable link to implement the desired functionality
CODE UPDATED WITH MY WORKING JS
<script type="text/javascript" language="javascript">
function checkAllBoxes() {
var gvControl = document.getElementById("gvSample");
//this is the checkbox in the item template.
var gvChkBoxControl = "chkSelectItem";
//Header Template checkbox.
var mainChkBox = document.getElementById("chkBoxAll");
//Array of Inputs in gridview.
var inputTypes = gvControl.getElementsByTagName("input");
for (var i = 0; i < inputTypes.length; i++) {
//if the input type is a checkbox and the id of it is what we set above
//then check or uncheck according to the main checkbox in the header template
if (inputTypes[i].type == 'checkbox' && inputTypes[i].id.indexOf(gvChkBoxControl, 0) >= 0)
inputTypes[i].checked = mainChkBox.checked;
}
}
GRIDVIEW CODE
<asp:TemplateField>
<HeaderTemplate>
<table style="width: 15px" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Label ID="lblXls" runat="server" Text="xls"></asp:Label>
<br />
<input id="chkBoxAll" type="checkbox" onclick="checkAllBoxes()" />
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelectItem" runat="server" />
</ItemTemplate>
</asp:TemplateField>
Thanks!
Try changing your parseInt to something like this to see if it helps at all. I know it's only a small change, but small things tend to break JS code:
var totalChkBoxes = parseInt("<%=gvTest.Rows.Count%>");
Secondly, if you have runat="server" on the checkbox in the header, you may need to change this line if your JSFunction:
var mainChkBox = document.getElementById("<%=chkBoxAll.UniqueID%>");

SelectAll in gridview using javascript on postback issue

i have a gridview which has a checkbox column. The header is of this column is a checkbox.
When it is checked all the values get checked and vice-verse.
I do this using javascript.
The problem is if i check it and perform any other event on the page which requires a postback the checked values disappear. I dont want them to disappear.
here is my code:
<script type="text/javascript">
function checkAllBoxes() {
//get total number of rows in the gridview and do whatever
//you want with it..just grabbing it just cause
var totalChkBoxes = parseInt('<%= GridView1.Rows.Count %>');
var gvControl = document.getElementById('<%= GridView1.ClientID %>');
//this is the checkbox in the item template...this has to be the same name as the ID of it
var gvChkBoxControl = "Select_CheckBox";
//this is the checkbox in the header template
var mainChkBox = document.getElementById("chkBoxAll");
//get an array of input types in the gridview
var inputTypes = gvControl.getElementsByTagName("input");
for (var i = 0; i < inputTypes.length; i++) {
//if the input type is a checkbox and the id of it is what we set above
//then check or uncheck according to the main checkbox in the header template
if (inputTypes[i].type == 'checkbox' && inputTypes[i].id.indexOf(gvChkBoxControl, 0) >= 0)
inputTypes[i].checked = mainChkBox.checked;
}
}
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="chkBoxAll" type="checkbox" onclick="checkAllBoxes()"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="Select_CheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<!-- The rest of your rows here -->
</Columns>
</asp:GridView>
Thanks for the help.
Make your checkbox a server-side control that uses the view state by adding runat="server". It will then maintain its value across post backs.
<input id="chkBoxAll" type="checkbox" onclick="checkAllBoxes()" runat="server"/>
And change your JavaScript to select the id that ends in chkBoxAll. I use jQuery in the example below:
//this is the checkbox in the header template
var mainChkBox = $('input[id$="chkBoxAll"]');
However, if you sort your gridview rows or use paging, you will likely come across less friendly behavior.

Categories