I have an asp.net application for membership management. One page needs to have a gridview which is populated based on a dropdown list of statuses. I initially thought about hard-coding with a Select Case, but then remembered that the dropdown list is databound and needs to be dynamic (because the admin-level users have another page to change the statuses). I'm still new at this, and my searches are not turning up anything.
Any links or examples would be helpful. Thanks.
I would suggest to use OnSelectedIndexChanged event of dropdownlist for your purpose with AutoPostBack property set to true, something like this
<asp:DropDownList runat="server" ID="ddlStatus" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
And on your code behind page you can bind your grid differently for different selected values in your event handler, something like this
protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlStatus.SelectedItem.Value == "RequiredValue")
{
// bind grid in some way
}
else
{
// bind grid in some other way
}
}
This will work irrespective of your binding the dropdownlist options dynamically or having them hard coded.
Related
I have a FormView which I populate using SqlDataSource1. I do databinding like:
Text='<%# Bind("EffectiveDate") %>'
FormView also contain a dropdownlist with a custom SqldataSource2. Dropdown list contains list of cities. User is able to change a value in dropdown.
Once I submit a form I have to send to PowerShell script some parameters which I supposed to hide like street, ZipCode, etc... On first form load I can bind this parameters to hidden fields from SqlDataSource1. But if user change a value in dropdownlist I have to rebind parameters. Have no idea yet how.
Thanks!
You can configure the drop down list to do a post back.
AutoPostBack="true"
and then on Page Load or Selection changed event you can set up the hidden field.
Example:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
hfSomething.Value = ddlSomething.SelectedValue;
}
else
{
//data binding code
}
}
I need to modify a 3-level dropdown <select> menu to function directly on the page and not inside an iframe (that's how we currently do it).
Here's how this menu works:
User makes a selection in 1st dropdown
Choices in 2nd dropdown are filtered based on what's chosen in 1st one; user selects from one of these choices
Choices in 3rd dropdown are filtered based on what's chosen in 2nd one; user selects from one of these choices; this last dropdown submits the form and redirects the user to another page, also passing the values from all three dropdowns.
Right now, this is accomplished using an <iframe> that queries an ASP.net database and reloads itself after each selection.
I really need it to work without using an iframe. Not sure what the most elegant way to approach this is ...
If you're open to using the ASP.NET Ajax Toolkit, they have a cascading dropdown control which does what you ask. One additional benefit, because it uses Ajax it does not have to reload the page after each selection in the dropdowns.
i think you'll probably need to use the 'onSelectIndexChanged` even on the DropDownList control. something like...
<asp:DropDownList id="ddl1" runat="server" OnSelectedIndexChanged="ddl1_OnSelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList id="ddl2" runat="server" OnSelectedIndexChanged="ddl2_OnSelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList id="ddl3" runat="server" OnSelectedIndexChanged="ddl3_OnSelectedIndexChanged"></asp:DropDownList>
Page_Load()
{
if(!IsPostBack)
{
ddl1.DataSource = getdata();
ddl1.DataBind();
}
protected void ddl1_onSelectedIndexChanged(object sender, EventArgs e)
{
ddl2.DataSource = getData(ddl1.SelectedValue);
ddl2.DataBind()
}
protected void ddl1_onSelectedIndexChanged(object sender, EventArgs e)
{
ddl3.DataSource = getData(ddl2.SelectedValue);
ddl3.DataBind()
}
protected void ddl3_onSelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("SomePage.aspx?ddl1="+ddl1.SelectedValue+"&ddl2="+ddl2.SelectedValue+"&ddl3="+ddl3.SelectedValue, true);
}
You could put all 3 of them in an UpdatePanel.
I am sure your changes are already making the page refresh.
Putting them in an update panel will make only the panel refresh, causing a partial page update.
This uses ajax to do the job internally.
Here is a update panel example, if you want one.
P.s.: Asp.net's method to create a <select> is a DropDownList control. I hope you are already using those, if not convert your selects to a dropdownlists.
I renew parameters of my DataSource for GridView on Button and loads event Page_Load()
and After it (or with it) GridView renews itself.
I need to rename Header rows but there is no event after Page Load.
more information about details here :
[On Button Click] I change DataSource and Bind it (SqlDataSource1.DataBind(); ) Then sure Page gone to Refresh elements.
[On Page_Load] GridView is changing data, but if I can't change Headers there because it's looking like loads after this function :(
[one more Button] I can add new button with a function - rename headers and it works correct everytime
protected void Button2_Click(object sender, EventArgs e)
{
// Stack overflow
DataRow T;
if (go)
if (GridView2.HeaderRow.Cells.Count != 0)
for (int i = 0; i < SQF.SQF.Permission.Rows.Count; i++)
{
T = SQF.SQF.Permission.Rows[i];
GridView2.HeaderRow.Cells[i].Text = (string)T["Caption1"];
WebMsgBox.Show((string)T["Caption1"]);
}
}
Trouble : I no need one more button for it, I need to rename header rows after I click button to change Data but I have no idea where I can do it.
thank you.
Well you could use Gridview DataBound event, like that:
<asp:GridView runat="server" ID="SomeID" OnDataBound="GridVIew_OnDataBound" ... />
The DataBound event occurs right after the databinding process for that particular control has completed.
After Page_Load there is an event Page.PreRender.
Maybe it will suit you.
EDIT.
All events of your components, such as GridView, are fired between Page.Load and Page.PreRender. Consider using RowDataBound and RowUpdated events.
I am building a pop-out menu, and the client wants it to be able to pop out continously based on a heirarchy.
For example, the first pane is a list of options. When they are hovered over, another pane should pop up next to it with the next level of options, and so on until the last level of options is reached.
I can handle all the javascript and stuff, but I can't think of a way to continously embed repeaters inside repeaters. I know I could do it once by putting a repeater inside another, but then I would only have two layers.
I need to be able to continously embed repeaters for each layer of options, or achieve this with a similar technique using a different control.
Any help is great, thanks!
You won't be able to build this in mark up. You'll have to add the controls dynamically in your code behind by building the Repeater for each level and adding it to the previous Repeater's template. It will require a full postback for each option selected because the nested Repeater could potentially be of different depth depending on which option is chosen.
You might be better off doing this all client-side, though, using AJAX and javascript. When an option is chosen, fire off an AJAX request to see if that option has sub-options. If it does (return them), then dynamically build the new options control using javascript and add it to the page. When a different option is chosen, you'll remove the elements from the DOM holding the previously chosen options sub-options.
If you can get your menu out in form of a list of MenuItem objects, each of which has a (sometimes empty) list of sub items (and I really mean a List<MenuItem> here... we're going to use this collection as a datasource for a sub-repeater, so it needs to implement IEnumerable<T>) as a property MenuItem.SubItems, you could probably make use of a UserControl that loops out one menu level, and calls upon itself for the next.
In the UserControl you'd have something like this:
<li><a href='<%= this.MenuItem.Url %>'><%= this.MenuItem.LinkText %></a></li>
<asp:Repeater ID="UCRepeater" runat="server">
<HeaderTemplate>
<ul>
<ItemTemplate>
<menu:MenuItem ID="MenuItemUC" runat="server" />
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
The UserControl in the ItemTemplate is the same one, so for each item template the same thing will be rendered.
Below is the Code Behind for this user control, and this is where the magic happens:
public partial class MenuItemUserControl : UserControl
{
// A property we'll use as the data source
public MenuItem MenuItem { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
// If the current menu item has sub items, we bind the repeater to them
// And by the way, there is no use doing this on every postback. First
// page load is good enough...
if(!Page.IsPostBack) {
{
if(MenuItem.SubItems.Count > 0)
{
UCRepeater.DataSource = MenuItem.SubItems;
UCRepeater.DataBind();
}
}
}
protected void UCRepeater_OnItemDataBound(object sender,
RepeaterDataBoundEventArgs e)
{
// Every time an Item is bound to the repeater, we take the current
// item which will be contained in e.DataItem, and set it as the
// MenuItem on the UserControl
// We only want to do this for the <ItemTemplate> and
// <AlternatingItemTemplate>
if(e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
var uc = (MenuItemUserControl)e.Item.FindControl("MenuItemUC");
if(uc != null)
{
// This is the magic. Abrakadabra!
uc.MenuItem = (MenuItem)e.DataItem;
}
}
}
}
So in order to get this to work, the only thing missing is really a nice way of getting your data out as a hierarchical list of MenuItems. This I'll leave to your data access layer (and it would be cheap easy using LINQ to SQL or Entity Framework... ;) )
DISCLAIMER: This code is provided as is, and I wrote it off the top of my head. I have not tested it, but I think it will work - and if it doesn't, it could at least give you an idea of how to solve the problem. If you have problems, please post them in comments and I'll try to help out - but there are no promises of success here. Just a willingness to help out! =)
I read about the dynamic control creation in ASP.NET this piece of text:
...When using dynamic controls, you
must remember that they will exist
only until the next postback. ASP.NET
will not re-create a dynamically added
control. If you need to re-create a
control multiple times, you should
perform the control creation in the
Page.Load event handler. This has the
additional benefit of allowing you to
use view state with your dynamic
control. Even though view state is
normally restored before the Page.Load
event, if you create a control in the
handler for the Page.Load event,
ASP.NET will apply any view state
information that it has after the
Page.Load event handler ends. This
process is automatic ...
I wanted to try it on example
create a button declaratively -
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
and dynamically on behind code 5 checkboxes -
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 5; i++)
{
var chBox = new HtmlInputCheckBox();
Controls.Add(chBox);
}
}
But when i check some checkboxes and hit the button, after postback all checkboxes
states are erased. It mean ASP.NET does not manage view states of dynamic controls
automatically? I tried to enable view state to each of checkbox and for whole page,
but its doesn't work.
Can someone explain:
1. Why is it so?
2. How to avoid this?
The reason this is happening is because in order for ASP.NET to restored POSTed values, those controls need to be a part of the page before Load. In order to make this work you need to (if possible) create your controls OnInit of the page.
The controls can be created on Page_Init.
protected void Page_Init(object sender, EventArguments e)
{
//Generate the checkboxes dynamically here.
CheckBox c;
for (int i = 0; i < 5; i++) {
c = new CheckBox();
c.ID = "Checkbox" + i.ToString();
divContainer.Controls.Add(c); //create a div with runat="Server" attribute or use an asp:Panel, etc. container controls.
}
}
After that, try clicking the button again, the state will be always be maintained.
You must set an ID for each dynamic control so that they can be synchronized across postbacks.
As I understand - there is no matter where to create controls in OnInit or OnLoad
(but some books suggests in onLoad), the matter is where to place them - if
you place through Controls.Add - it place them out of <form></form> so postback
does not takes control's states. after cretating a placeholder inside <form></form> and add dynamic controls to this placeholder everthing start to work fine.