I want to show different Gridviews when I select values from a dropdownlist.I use Visible="False" property to gridviews and I want to show only one on every value updated.
Example: when I select the Value "Points" I want to show GridView1 and when I select Names I want to show "GridView2". This is my ASP:
<asp:DropDownList ID="Stats_Ddl" runat="server" AutoPostBack="True" OnSelectedIndexChanged="Stats_Ddl_IndexChanged" >
<asp:ListItem>POINTS</asp:ListItem>
<asp:ListItem>NAMES</asp:ListItem>
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" Visible="False"> blahblahblah1</asp:GridView>
<asp:GridView ID="GridView2" runat="server" Visible="False">blahblahblah12</asp:GridView>
and c# is:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Stats_Ddl_IndexChanged(object sender, EventArgs e)
{
}
Any suggestions for c# code? thanks...
Hi inside event SelectedIndexChanged
you should put for example
If(Ddl.SelectedValue == "1"){
GridView1.Visible = true;
GridView2.Visible = false;
}else{
GridView1.Visible = false;
GridView2.Visible = true;
}
i see that you have a ListItem don't forget give to a value to property "Value" for example
Point = 1
Name = 2
You can use SelectedItem.Text property of DropDownList to branch
protected void Stats_Ddl_IndexChanged(object sender, EventArgs e)
{
if(Stats_Ddl.SelectedItem.Text == "POINTS")
GridView1.Visible = true;
else
if(Stats_Ddl.SelectedItem.Text == "NAMES")
GridView2.Visible = true;
}
Related
i am trying to make a "clock in / out site" and one of the functions is for the user to edit old clock ins/outs. i have added a dropdown with 2 values "IND" and "UD". How do i get these values out, and send them to my "CHECKIN" sql value. I hope you know what iam trying to explain.
this is what it looks like:
The Dropdown:
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"
OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem Selected="True">Ind</asp:ListItem>
<asp:ListItem Value="UD">Ud</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
what ive tried:
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
string val = row.Cells[3].ToString();
SqlDataSource1.SelectParameters.Add("CHECKEDIN", val);
}
and aswell as:
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)GridView1.FindControl("DropDownList2");
string val = ddl.SelectedValue;
SqlDataSource1.SelectParameters.Add("CHECKEDIN", val);
}
Both of these return "null" tho
Use RowDataBound event of GridView.
Code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow
&& GridView1.EditIndex == e.Row.RowIndex)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList2");
string val = ddl.SelectedValue.ToString();
SqlDataSource1.SelectParameters.Add("CHECKEDIN", val);
}
}
You can cast the sender back to a DropDownlist and get the selected value
DropDownList dropDownList = sender as DropDownList;
string val = dropDownList.SelectedValue;
But how will you know with this method which row the DropDownList belongs to?
You can use a trick for that, like abusing the CSS class to get the correct row number.
<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true" CssClass='<%# Container.DataItemIndex %>'>
And get the row number in code behind.
int rowNumber = Convert.ToInt32(dropDownList.CssClass);
I am looking for this over the internet but I am not getting any suitable answers. Here is my problem:
I have a web user control which has the following three controls
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="true"
CssClass="radioButton" RepeatDirection="Horizontal" RepeatLayout="Flow"
onselectedindexchanged="RadioButtonList1_SelectedIndexChanged" >
<asp:ListItem Value="UET111">Use Existing</asp:ListItem>
<asp:ListItem Value="CNT111">Create New</asp:ListItem>
</asp:RadioButtonList>
<asp:DropDownList ID="DropDownList1" runat="server" >
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" C></asp:TextBox>
It's code behind
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.Visible = false;
TextBox1.Visible = false;
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedItem.Value == "UET")
{
DropDownList1.Visible = true;
TextBox1.Visible = false;
}
else if (RadioButtonList1.SelectedItem.Value == "CNT")
{
DropDownList1.Visible = false;
TextBox1.Visible = true;
}
}
Now my question-
Actually I am using this web control 5 times on the same aspx page. The problem is I want to fill the dropdown list from aspx page and each dropdown will have different data(as I have 5 user control).
Is it possible to do that? Please help me with these and let me know if anyone knows a better way of doing this.
Create a public function in your user control that lets you send in a DataSource for the dropdown list to populate from, and if you want bind as well.
public BindDropdownDataSource(DataSet dataSource)
{
DropDownList1.DataSource = dataSource;
DropDownList1.DataBind();
}
I want to enable a panel based on dropdownlist selected value.
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddl.SelectedValue == "A")
{
lnk.Style.Add("Display", "block");
panel1.Visible=true;
panel1.Enabled=true;
}
}
The panel is not getting displayed. I have set autopastback property of dropdownlist to true.Can someone please help me.
should be without the quotes:
panel1.Enabled = true;
but if you wanted to show hide a panel:
<asp:Panel ID="panel1" runat="server" Visible="False" >
...
</asp:Panel>
then the right way would be
panel1.Visible = true;//false to hide
i have a gridview which is inside an update panel. when i click "cance" after the edit, nothing happens. When i debug it does go into my gvWorkhours_RowCommand function and inside the cancel if, but nothing happens on screen (the edit fields are all still visible)
here is what i have:
<asp:GridView ID="gvWorkhours" runat="server" AutoGenerateColumns="false" CssClass="GridViewStyle" OnRowEditing="gvWorkhours_RowEditing"
OnRowCommand="gvWorkhours_RowCommand" >
<EmptyDataTemplate>
no data returned
</EmptyDataTemplate>
<Columns>
<asp:commandfield buttontype="Link" showeditbutton="true" edittext="Edit" />
<asp:TemplateField HeaderText="Organization">
<ItemTemplate>
<%# Eval("org.orgCode").ToString() + "- " + Eval("org.orgSubCode").ToString()%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Year-Qtr">
<ItemTemplate>
<%# Eval("year").ToString() + "- " + Eval("qtr").ToString()%>
</ItemTemplate>
</asp:TemplateField>
.......
protected void gvWorkhours_RowEditing(Object sender, GridViewEditEventArgs e)
{
populateGrid(); //pulls from the Db and binds to the grid
gvWorkhours.EditIndex = e.NewEditIndex; //This is the selected row to edit
gvWorkhours.DataBind(); //Make the edit Template show up
}
protected void gvWorkhours_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if (e.CommandName == "Cancel")
{
gvWorkhours.EditIndex = -1;
populateGrid();
}
}
private void populateGrid()
{
//getting all variables for update here.
Workhours wh = new Workhours(selectedItem, year, qtr);
gvWorkhours.DataSource = wh.exposures;
gvWorkhours.DataBind();
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
// throw(ex);
}
}
what am i missing here?
You don't have the UpdatePanel shown in your example, so I don't know how you have it set up, but if you have the UpdateMode set to Conditional, you probably need to manually call the Update method on the update panel:
if (e.CommandName == "Cancel")
{
gvWorkhours.EditIndex = -1;
populateGrid();
UpdatePanel1.Update(); // whatever the name of the UpdatePanel is
}
try to give other word than cancel like "CancelRecord" and then try with it
Yes it's an old question, but no answer.
Cancel needs to be in a RowCancelEdit method ... seems 'Cancel' is a protected word in this scenario
protected void gvResults_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView gv = (GridView)sender;
gv.EditIndex = -1;
BindGrid2();
}
I have a page with a dropdownlist and a button on it. The initial selection on the dropdown is an empty string. I do not want this submitted to the server so I disable the button. I then want to enable the button if any other selection is made on the dropdown. However my ddlBusinessUnit_SelectedIndexChanged method is never hit when I make changes in the dropdown list.
html:
<asp:DropDownList ID="ddlBusinessUnit" EnableViewState="true" runat="server"
OnSelectedIndexChanged="ddlBusinessUnit_SelectedIndexChanged" />
code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dsDate.Date = DateTime.Today;
PopulateBusinessUnits();
StatusMessages.Visible = false;
}
bGetFiles.Enabled = false;
}
public void ddlBusinessUnit_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlBusinessUnit.SelectedItem.Text != "")
bGetFiles.Enabled = true;
}
Set AutoPostBack="true" for your dropdown.
<asp:DropDownList ID="ddlBusinessUnit" EnableViewState="true" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlBusinessUnit_SelectedIndexChanged" />
you're missing AutoPostBack="true" on your asp dropdown control