I'm using dropdownlist in asp.net which contains 10,20,50 as values.
I'm using gridview to display dataretrieve from table based on the value selected in dropdownlist.
Example, when I select 20, the gridview should display only 20 rows.
I'm using the following coding;
protected void ddlRowPerPage_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox txtCurrentPage = sender as TextBox;
DropDownList ddlRowPerPage = sender as DropDownList;
int startRowIndex = 0;
pager.PageSize = Convert.ToInt32(ddlRowPerPage.SelectedValue);
Response.Cookies[hdnRowPerPageName .Value].Value = pager.PageSize.ToString();
pager.SetPageProperties(startRowIndex, Convert.ToInt32(ddlRowPerPage.SelectedValue), true);
}
I've 30 rows in table.
My problem is, when I select 50, it shows all rows. But when I select 10, the SelectedIndex function is not firing.
At the same time, after selecting 50, when I select 20, the selectedindex is firing.
What is the problem?
Here is the updates .aspx page coding:
<div class="pull-left">
<asp:DropDownList ID="ddlRowPerPage" runat="server" OnSelectedIndexChanged="ddlRowPerPage_SelectedIndexChanged"
EnableViewState="true" AutoPostBack="true" Width="60px" >
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="20">20</asp:ListItem>
<asp:ListItem Value="50">50</asp:ListItem>
</asp:DropDownList>
rows per page
</div>
Problem : as your items order is 10,20,50...etc., it is cleared that 10 is at first location and when you select the 10 as first selection Index will not get changed.
Reason: IndexChanged Event only fires when SelectedItem Index is changed.
ut when you select some other item 20 or 50 and then select 10 it definitely fires.
Solution :
Add a Default item to DropDownList as --Select Item-- so that whenever user selects item 10 it fires the event as Selected Index is changed.
Try This:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Height="28px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="201px">
<asp:ListItem>-Select Item-</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>50</asp:ListItem>
</asp:DropDownList>
The Problem is in not understanding when this event can be fired.. it can fired only when the index of the dropdownlist changed and that didn't happened in your case, because the first element when the dropdownlist rendered is 10.
In my humble opinion, the optimum solution according to that issue is rendering the dropdownlist in the page with a primer default selection like that :
<div class="pull-left">
<asp:DropDownList ID="ddlRowPerPage" runat="server" OnSelectedIndexChanged="ddlRowPerPage_SelectedIndexChanged"
EnableViewState="true" AutoPostBack="true" Width="60px" AppendDataBoundItems="true">
<asp:ListItem Value="0">--Choose--</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="20">20</asp:ListItem>
<asp:ListItem Value="50">50</asp:ListItem>
</asp:DropDownList>
rows per page
</div>
protected void ddlRowPerPage_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox txtCurrentPage = sender as TextBox;
DropDownList ddlRowPerPage = sender as DropDownList;
int startRowIndex = 0;
pager.PageSize = Convert.ToInt32(ddlRowPerPage.SelectedValue);
Response.Cookies[hdnRowPerPageName.Value].Value = pager.PageSize.ToString();
pager.SetPageProperties(startRowIndex, Convert.ToInt32(ddlRowPerPage.SelectedValue), true);
}
Add ViewStateMode="Enabled" to the DropDownList it should work.
I got information from here quite often.
I just want to share my experience this time and hope it might help someone someday.
I have a DropDownList in my GridView's PagerTemplate.
I used the example code from http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pagertemplate(VS.80).aspx
I used it in many of my pages. All works fine except one.
The OnSelectedIndexChanged event just isn't fired in that one page.
Later I set the "EnableViewState" of that GridView to false, it works finally.
Related
I am using 3 DropDownList inside DataList. So, each row contains 3 DropDownList. DataList also contains DataKeyField.
Now, if user select value from DropDownList1 then I want to bind DropDownList2 and if user select value from DropDownList2 then I want to bind DropDownList3. I am using SelectedIndexChanged and I am able to get value of related DropDownList selected value. But, If user select DropDownList2 then I also need value of DropDownList1 and also need value of respected DataKeyField.
How to get this ???
My code Sample:
<asp:DataList ID="dlTest" runat="server" OnItemDataBound="dlTest_ItemDataBound"
DataKeyField="TestId">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"> </asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged"> </asp:DropDownList>
</ItemTemplate>
</asp:DataList>
Here, onSelectedIndexChanged of DropDownList3, I am able to get selectedValue of it but I also need respected row's selectedValue of DropDownList1 and DropDownList2 and also respected DataKeyField value.
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
var DropDownList3 = (DropDownList)sender;
string DDL3 = ((DropDownList)DropDownList3.NamingContainer.FindControl("DropDownList3")).SelectedValue;
// Also need selectedValue of DropDownList1 and DropDownList2 and DataKeyField.
}
I think you are almost there. You just need to. Using your code:
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
var DropDownList1 = (DropDownList)sender;
var DropDownList2 = (DropDownList)sender;
var DropDownList3 = (DropDownList)sender;
string DDL1 = ((DropDownList)DropDownList1.NamingContainer.FindControl("DropDownList1")).SelectedValue;
string DDL2 = ((DropDownList)DropDownList2.NamingContainer.FindControl("DropDownList2")).SelectedValue;
string DDL3 = ((DropDownList)DropDownList3.NamingContainer.FindControl("DropDownList3")).SelectedValue;
}
Either way I would do it a little bit different:
item valueddl1 = DropDownList1.SelectedValue;
And so on; which should work as weel and it is a little bit more simple.
hi i have gridview that when a row is selected, it populates into textboxes that are used to populate the gridview itself. the last field is a dropdownlist and its not displaying when the gridview is clicked. i set a breakpoint and see that its stuck on the first - 0 index. i dont know why it isnt moving forward... here is the code:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
....
if (DropDownListCurrency.Items.FindByValue(row.Cells[7].Text.ToString().Trim) != null)
{
DropDownListCurrency.SelectedValue = row.Cells[7].Text.ToString().Trim();
}
....
}
<asp:DropDownList ID="DropDownListCurrency" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
why you want take the value from textbox. Will better use DataKeyNames like this inside that event
GridViewRow row = GridView.SelectedRow;
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Value);
this work if you have only one value in DataKeyName if you look not there a index if you want have more than one value use this
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Values["FirstValue"]);
string name = Convert.ToString(GridView.DataKeys[row.RowIndex].Values["SecondValue"]);
Does the DropDownList contains the words that you wanna show displayed?
You need to set the AutoPostBack property to True:
it should be :
<asp:DropDownList ID="DropDownListCurrency" AutoPostBack="True" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
made a simple error...
called the wrong field was supposed to be value (Currency_ID) and not the text (Currency)
DropDownListCurrency.SelectedValue = GridView1.DataKeys[row.RowIndex].Values["Currency_ID"].ToString().Trim();
Within my EditTemplate I have a DropDownList as such:
<EditItemTemplate>
<asp:DropDownList ID="ddlFixture" runat="server" AutoPostBack="True" onselectedindexchanged="fixtureSelected"
DataSourceID="FixtureDataSource" DataTextField="WTag" DataValueField="WTag" AppendDataBoundItems="true" SelectedValue='<%#Bind("Fixure") %>'>
</asp:DropDownList>
</EditItemTemplate>
When the user selects an item from the DropDownList, I like to populate other fields in that are in EditMode as well.
You will notice that on onselectedindexchanged="fixtureSelected" I am calling fixtureSelected. I have noted some of the issues I am facing:
protected void fixtureSelected(object sender, EventArgs e)
{
// Below I am trying to get value of ddlFixture but it cannot recognize ddlFixture. GettingThe name 'ddlFixture' does not exist in the current context
string fixtureId = ddlFixture.SelectedItem.Value;
// I also need to update the text in EditMode but this will not work either. Get similar message as for ddlFixture
txtCampus.Text = "Campus1";
}
Your problem is due to the fact that the controls your are trying to access are inside the edititemtemplate of your GridView.
To get to the dropdownlist instance, you can use:
DropDownList ddlFixture = sender as DropDownList;
And to get the other controls in edititemtemplate, you use:
ddlFixture.NamingContainer.FindControl("control_id")
For example:
TextBox txtCampus = ddlFixture.NamingContainer.FindControl("txtCampus") as TextBox;
I want to show multiple text boxes depending on the number a user selects from a dropdown box. Example below:
So whatever number is selected in the drop down the page refreshes and displays that number of text boxes. I need to go up to 20 fields. Is there a way to do this in C#, or maybe with the Ajax Control Toolkit?
ASPX
<asp:Label ID="NumAccounts" runat="server" Text="# of Accounts"></asp:Label> <asp:DropDownList
ID="EmpNameList" runat="server" onselectedindexchanged="NumAccountsList_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
sure there is.
use:
int ctrlCount=Convert.ToInt32(DropDownList1.SelectedItem.Value);
int ctrlTopPos=30;
lbl_name.Text="Name:";
for(int i=0;i<ctrlCount;i++)
{
Label lbl_name=new Label();
TextBox txt_cur=new TextBox();
txt_cur.Top=ctrlTopPos+(i*30);
lbl_name.top=ctrlTopPos+(i*30);
txt_cur.left=lbl_name.Width+30;
Panel1.Controls.Add(lbl_name);
Panel1.Controls.Add(txt_cur);
}
Create an asp:panel and name it Panel1.
Put the given code inside the SelectedIndexChanged event of your dropdownlist.
set the autopostback property of your dropdownlist to true.
it will work.
hope that helps.
try this
<asp:Label ID="NumAccounts" runat="server" Text="# of Accounts"></asp:Label> <asp:DropDownList
ID="EmpNameList" runat="server" onselectedindexchanged="NumAccountsList_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<div>
<asp:PlaceHolder id="ContentPlaceHolder1" runat="server" />
</div>
protected void NumAccountsList_SelectedIndexChanged(object sender, EventArgs e)
{
ContentPlaceHolder1.Controls.Clear();
for(i=0; i<Convert.ToInt32(EmpNameList.SelectedItem.Value); i++)
{
TextBox tx= new TextBox();
tx.ID="tx"+i;
ContentPlaceHolder1.Controls.Add(tx);
ContentPlaceHolder1.Controls.Add(new LiteralControl("<br />"));
}
}
I have gridview with Employee Name, Emp Code, Designation. I am populating Employee Names dropdownlist with values in rowdatabound event. In edit mode of, on selection of a value in Employee Names dropdownlist, the EmpCode and Designation should change accordingly. The empCode and Designation label controls are in templatefield. I have writtern selectionchanged event for the Employee Names drodownlist,
ddlEmp.SelectedIndexChanged += new EventHandler(grd_ddlEmp_SelectedIndexChanged);
but I do not know how to change Emp Code and Designation values inside the particular row in gridview.
Does these things inside the SelectedIndexChanged event of the DropDownList
First find the row.
Then access the controls and change accordingly.
Pseudo Code
protected void grd_ddlEmp_SelectedIndexChanged(object sender, EventArgs e)
{
GridView row = ((DropDownList)sender).NamingContainer as GridViewRow;
Label Designation = row.FindControl("id of the designation label") as Label;
Designation.Text = "new Designation Name";
}
In the SelectedIndexChanged function, find the selected value of the drop down box
DropDownList ddl = (GridView1.Rows[GridView1.SelectedIndex].FindControl("DropDownList1") AS DropDownList);
var selectedVal = ddl.SelectedValue;
Execute some code to determine the Emp Code and Desgination and then populate the relevant label ( or other control):
Label lbl = GridView1.Rows[GridView1.SelectedIndex].FindControl("Label1") as Label;
Assign value to the label.
You need to put your gridview in UpdatePanel and do it in code behind of SelectedIndexChanged event like I have done in datalist below :
<asp:DataList ID="dlstPassengers" runat="server" OnItemDataBound="dlstPassengers_ItemDataBound"
RepeatDirection="Horizontal" RepeatColumns="2" Width="100%">
<ItemTemplate>
<div class="form-linebg" style="line-height: 32px;">
<asp:DropDownList ID="ddlCountry" AutoPostBack="true" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"
CssClass="select-3" Style="width: 145px; margin-bottom: 9px;" runat="server">
</asp:DropDownList>
<br />
<asp:DropDownList ID="ddlCity" CssClass="select-3" Style="width: 145px; margin-bottom: 10px;"
runat="server">
</asp:DropDownList>
</div>
</ItemTemplate>
</asp:DataList>
In code behind :
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlCountry = (DropDownList)sender;
DropDownList ddlCity = (DropDownList)((DropDownList)sender).Parent.FindControl("ddlCity");
BindCity(ddlCity, ddlCountry.SelectedValue);
}
private void BindCity(DropDownList ddlCity, string countryCode)
{
// Binding code of city based selected country
}
You need to set EmpCode and Designation columns on SelectedIndexChanged event of dropdown by finding control in code behind.
use in your dropdown AutoPostBack="true"