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
}
}
Related
I have an webforms ASPX page, which has a dynamically added user control. The user control has two DropDownLists, one is in an UpdatePanel as its items depend on the selection in the first DropDownList.
The problem is that if you do not change the value of the first DropDownList the value of the second DropDownList does not get saved. If you do change the value of the first DropDownList then it works fine.
This is how it works briefly...
The first time the page loads, the previous values are set. This happens in the main ASPX page Page_Load event where the user control is dynamically added and the initial values are set through a property of the user control. The property sets the selected value of the first DropDownList, triggers the SelectedIndexChanged event which populates items in the second DropDownList with choices based on the first DropDownList selection, and then selects the previous value of the second DropDownList.
Then in the main ASPX page, the user control is dynamically added again on post back in the PreLoad event.
Viewstate is fully enabled throughout.
I have debugged and on post back the second DropDownList has no Items. Even during the UpdatePanel partial post back the items collection is empty. However, if the first DropDownList is changed then the Items collection is correct - it's only if the second DropDownList is last populated on the initial load that the problem happens.
Here's the relevant parts of the code...
Any help greatly appreciated.
ASPX page:
protected void Page_PreLoad(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
PopulateTemplateOptions();
}
}
protected void PopulateTemplateOptions(bool init = false)
{
//this is where the control is dynamically added
//If not post back then initial values are set by passing values to
//user control's TemplateOptions property
}
User control:
public string TemplateOptions
{
set
{
//this is where the initial values are set
}
}
protected void ddlEnquirySubjectDefault_SelectedIndexChanged(object sender, EventArgs e)
{
//this is where items are added to the second drop down list based on the selection in the first one.
}
(These are the only parts that are relevant, but there's another 7,000 lines of code I didn't include.)
Here are two of my ideas:
1) This line might be wrong in your code: if (Page.IsPostBack)
It must be if (!Page.IsPostBack), although I never used Page_PreLoad event so I am not sure if it is right.
2) Try to do what you want in Page_Init event instead. I use it on my projects, without problem.
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 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.
I have a hyperlink in a gridview which I want users to click on and it directs them to a particular page and also either passes in the first field of the gridview (the ID) or holds it in session, preferebly in session.
The link is just static text so no matter what record they click on i want to get them to the same page, but with that records ID available.
Just not sure how to add this to the NavigateUrl of the hyperlink.
ANy hints appreciated, thanks
You can easily generate the URL in the markup of your GridView without resorting to code. What you need to do is:
In the DataNavigateUrlFields
property of your HyperLinkField, put
the name of the column that contains
your id.
In the
DataNavigateUrlFormatString, put the
path to your page, plus the
querystring that the next page will
use to get the id, but where the
value should go, put {0} instead.
e.g.
<asp:Hyperlink DataNavigateUrlFields="ProductId" DataNavigateUrlFormatString="details.aspx?id={0} />
When the control is rendered at runtime, you will find that for each row, the {0} is replaced by the value of the ProductId column.
See String.Format and DataNavigateUrlFormatString for more details.
You can handle the Row_DataBound event to find the hyperlink control and update the NavigateUrl property.
You can add simple html control Text to link, it will produce same html.
Use HyperLink control and then write an event handler function for RowDataBound event, like this:
protected void OnRowDataBound(object source, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hyperLink = e.Row.FindControl("hyperLinkID") as HyperLink;
// example, adjust this to your needs.
hyperLink.NavigateUrl = "~/detail.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "ID");
}
}
Not sure why you have taken server control instead of tag of HTML.
two ways you can do it.
1)if it is an static link just prefix the page name and append the id in mark up.
for ex
<a href='myPage.aspx<%#Eval("YourID")%>'><strong>Click me to navigate</strong></a>
2)give some id to the a tag and make it runat server handle the data bound event and bind the value to it.
protected void MyGridview_ItemDataBound(object sender, ListViewItemEventArgs e)
{
HtmlAnchor AncImage = e.Item.FindControl("AncImage") as HtmlAnchor;
AncImage.href="myPage.aspx"/id=" + DataBinder.Eval(e.Row.DataItem, "ID"); ;
//the id is the value that you want to append for redirection
}
I´ve read most posts here but i can´t figure out why the "CheckedChanged" Event is not firing. Here is my situation.
I´m using a Repeater to generate Items out of a Database. Each ReapeaterItem should include an UpdatePanel, because i have to Update the Controls inside the UpdatePanel and do not want to reload the complete page. Inside these dynamically generated UpdatePanels (each RepeaterItem has one) i´m adding up to three Checkboxes dynamically (based on the Database). These Checkboxes need to fire the "CheckedChanged" event, because on some conditions i want to enable/disable/check/uncheck Checkbox1, 2 or 3 based on business logic. ... Hope you got this so far. I´m adding all Controls and have the EventHandler Added. But the generated Code does not reflect the Event Handler. I tried OnItemDataBound, OnItemCreated, PreRender, ... Events to add the Eventhandler too, but i was not able to find the CheckBox-Control with the ID.
I´m totally lost with this and on the way to use Buttons instead of Checkboxes. From what i read so far is that with Buttons i can use the CommandName from the Button and the ItemCommand-Event from the Repeater to get a workaround, but then i need to reflect the "Check" on the Page in some way.
btw, every Repeater (8) sits inside an ajaxtoolkit-accordion control.
Here i give you some Code:
aspx-Page
<asp:Repeater ID="RepeaterAccordionPane2" runat="server">
<ItemTemplate>
HTML Stuff<%# DataBinder.Eval(Container.DataItem, "Header")%>HTML Stuff<%# DataBinder.Eval(Container.DataItem, "Beschreibung")%></td>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode=Conditional>
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
HTML Stuff
</ItemTemplate>
</asp:Repeater>
Here is the Page_Load Part
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dvAlleArtikel = new System.Data.DataView(...Database...);
[... some other code here ...]
RepeaterAccordionPane2.DataSource = dvAlleArtikel;
//RepeaterAccordionPane2.ItemCreated +=new RepeaterItemEventHandler(RepeaterAccordionPane2_ItemCreated);
//RepeaterAccordionPane2.PreRender +=new EventHandler(RepeaterAccordionPane2_PreRender);
RepeaterAccordionPane2.DataBind();
int nUpdatePanelIndex = 0;
foreach (Control crInRepeater in RepeaterAccordionPane2.Controls)
{
if (crInRepeater.GetType() == typeof(RepeaterItem))
{
foreach (Control crInRepeaterItem in crInRepeater.Controls)
{
if (crInRepeaterItem.GetType() == typeof(UpdatePanel))
{
LiteralControl litTabelleBeginn = new LiteralControl("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">");
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litTabelleBeginn);
if (dvAlleArtikel[nUpdatePanelIndex]["ArtNr1"].ToString() != "0")
{
CheckBox CheckBox1 = new CheckBox();
CheckBox1.ID = dvAlleArtikel[nUpdatePanelIndex]["ArtNr1"].ToString();
CheckBox1.Text = (dvAlleArtikel[nUpdatePanelIndex]["CheckBoxLbl1"].ToString() == "" ? "leer" : dvAlleArtikel[nUpdatePanelIndex]["CheckBoxLbl1"].ToString());
CheckBox1.AutoPostBack = true;
CheckBox1.CheckedChanged +=new EventHandler(CheckBox1_CheckedChanged);
LiteralControl litNeueTabellenZeileBeginn = new LiteralControl("<tr><td width=10><img src=\"images/helper/spacer.gif\" width=\"10\"></td><td height=\"20\">");
LiteralControl litNeueTabellenZeileEnde = new LiteralControl("</td><td width=\"100\" height=\"20\">" + dvAlleArtikel[nUpdatePanelIndex]["ArtPrice1"].ToString() + " € </td></tr>");
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litNeueTabellenZeileBeginn);
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(CheckBox1);
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litNeueTabellenZeileEnde);
}
[... some other code here...]
LiteralControl litTabelleEnde = new LiteralControl("</table>");
((UpdatePanel)crInRepeaterItem).ContentTemplateContainer.Controls.Add(litTabelleEnde);
nUpdatePanelIndex++;
}
}
}
}
This code is never reached:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
int foo = 0;
}
This is the CheckBox-Code generated:
<input id="AccordionPane2_content_RepeaterAccordionPane2_ctl00_6200" type="checkbox" name="AccordionPane2_content$RepeaterAccordionPane2$ctl00$6200" onclick="javascript:setTimeout('__doPostBack(\'AccordionPane2_content$RepeaterAccordionPane2$ctl00$6200\',\'\')', 0)" />
The Event is generated, but when i click the Checkbox all Content in the UpdatePanel is gone and the CheckedChanged-EventHandler is not fired.
What am i doing wrong?
Thanks to all advice, i´m really stuck.
mk
The first time the page loads you are adding all the checkboxes to the Controls collection, and they get rendered. When you do a postback (through the CheckBox's AutoPostBack) you have a check if(!IsPostBack) that doesn't allow the checkboxes to be added to the Controls collection on the postback. Because of that, you won't see the controls and the page, and when the page lifecycle tries to call the events (which occurs AFTER Page_Load), the controls that created the events are no longer there.
You will need to refactor your Page_Load method so it does two things - 1, regardless of the value of IsPostBack bind the repeaters and create the dynamic controls. 2, if IsPostBack==false, i.e., an initial load, then set the values of the dynamic controls. you don't want to set the values of the dynamic controls when IsPostBack==true because then you will lose the values the user entered.
also, just a note:
if (crInRepeater.GetType() == typeof(RepeaterItem))
can be rewritten as:
if (crInRepeater is RepeaterItem)