dropdown event handler writing in asp.net and C# - c#

What properties should I enabled in dropdown menu and how should I initiate the code in C#? I'm using Visual studio as my back end.

ASPX:
Set the auto postback and add an event handler:
<asp:DropDownList runat="server" id="foo" AutoPostback="true" OnSelectedIndexChanged="foo_SelectedIndexChanged>
</asp:DropDownList>
In the .CS
protected void foo_SelectedIndexChanged(Object sender, EventArgs e){
}

Hope this works for your requirements.
1. Getting dataset value from Database.
2. Biding for the required DropDownList
3. Selecting default value for the DropDownList.
Make Sure to check autopost back property to "True"
http://www.codeproject.com/Questions/400833/How-to-bind-or-populate-data-into-a-Dropdown-List

Related

ASP.NET viewstate update without javascript

Hy,
how to update my viewstate on model variable value changed? To make it more clear lets take an example.
I have 3 variables in my model. One of them is bool and other two are strings. when I run my application I have a form of one checkbox. When I click on checkbox I want those two string values appear as a input values and when I click on the checkbox again I want to make them disappear again.
How could I make this happen without javascript?
when Using simple web form application just use
on aspx page
<asp:CheckBox Text="Check" Checked="false" ID="MyCheckBox" runat="server" AutoPostBack="true" OnCheckedChanged="MyCheckBox_CheckedChanged" />
on aspx.cs page
protected void MyCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (MyCheckBox.Checked)
{
Response.Write("CheckeD");
}
else
{
Response.Write("Un CheckeD");
}
}

How to pass a value from function in aspx to cs.file

I am sure that I am missing a small thing. I have ddl and a function, i want to pass the 'value attribute' of a chosen ddl option to my function in cs file.
But I cant get the value attribute..
-I checked the DDL and the Value attribute is fine and helds my info well.
--I tried using the word - this. to get my value but it didnt work..
aspx page
<asp:DropDownList ID="myDdl" runat="server" onchange='<%# orgenaize('here I need the value attribute') %>'/><br />
aspx.cs file
public void orgenaizeCheckBox(string currentId)
{
//do something
}
You can add javascript event handler using Attributes.Add in code behind where you can easily use the value from dll.
myDdl.Attributes.Add("onchange", "orgenaize(" + dllClass.Attribute + ");");
<asp: DropDownList ID ="ddlValue" runat ="server" AutoPostBack="True"
OnSelectedIndexChanged="ddlValue_SelectedIndexChanged"
ToolTip ="Please Select value">
</asp: DropDownList>
Then in your .cs class just handle the event
protected void ddlPhoneModel_SelectedIndexChanged(object sender, EventArgs e)
{
// What ever you want to do on selected index change
ddlValue.SelectedItem.Value
}
.Value should work, long time since i played around with Web controls but that should work

Why does the selectedindexchanged event not work when making controls visible

Im trying to have my drop down list make four controls visible on the selectedindexchanged event for the control.
Basically when the user chooses "Server" from the drop down list the event should fire and the user should see two extra options.
Tried a tonne of approaches so far including text_changed event but nothing.
Here is what i got so far
//adds new fields to the form when the user selects server as the asset type
protected void AddNewFields(object sender, EventArgs e)
{
//If the asset is a server then make the extra controls available
if (TypeDDL.Text.Equals("Server"))
{
DNLabel.Visible.Equals(true);
//DNLabel.Visible = true;
DomainNameTB.Visible = true;
RoleLabel.Visible = true;
RoleDDL.Visible = true;
}
}
<asp:DropDownList ID="TypeDDL" runat="server" DataSourceID="AssetTypeDS"
DataTextField="AssetTypeDescription" DataValueField="AssetTypeID" OnTextChanged="AddNewFields">
</asp:DropDownList>
Add AutoPostback="True" to your DropDownList and the above code should trigger
As for an explanation: The drop down list doesn't automatically post back to the server. Changing a selection happens on the client side. Once you add the above, it'll repost the page. If you don't want the whole page to flicker each time someone changes a selection, you can either use some client side Javascript or Jquery, or use an asp:UpdatePanel
Please set AutoPostBack="True" property of a DropdownList...
add AutoPostBack="true" in dropdown
<asp:DropDownList ID="TypeDDL" runat="server" DataSourceID="AssetTypeDS" AutoPostBack="true"
DataTextField="AssetTypeDescription" DataValueField="AssetTypeID" OnTextChanged="AddNewFields">
</asp:DropDownList>

DropDownList events don't fire

I’m having trouble with my DropDownList. The events don’t fire! I've tested it on a separate project, with a DropDownList and a literal. Every time the selected value would change, I would add a little star “*” to the literal. No problems what so ever. But every time I try it on my webpage in the project, it fails.
Here is an image.
protected void ddlConsole_SelectedIndexChanged(object sender, EventArgs e)
{
ltlTesting.Text += "*";
}
UPDATE:
I've tried some things but still with no succes. I hope someone can tell me what i'm doing wrong. I'm wiring the events in the code behind now, but i've added a linkbutton next to the dropdownlist to see if it works.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ddlConsole.SelectedIndexChanged += new EventHandler(ddlConsole_SelectedIndexChanged);
lnkGet.Click += new EventHandler(ddlConsole_SelectedIndexChanged);
}
Here is an image to see what's going on. The stripe in the literal at the beginning is added in pageload with the same code the star is added. Just to be sure it doesn't load twice. The "GET" linkbutton works fine. The dropdownlist doesn't...
Have you Set
AutoPostBack="true"
in control properties??
EDIT:
Remove
OnSelectedIndexChanged="ddlConsole_SelectedIndexChanged"
from the markup in the ASPX page and try again only with AutoPostback true and the
event defined in codebehind. The aspx page should look like this:
<asp:DropDownList runat="server" ID="ddlConsole" AutoPostBack="True"></asp:DropDownList>
Is the AutoPostBack of the dropdownlist true ?
Check AutopostBack property of Dropdownlist set it to true :
If picture is right and the AutoPostBack="True", is there any code that sets the value of ltlTesting when page loads?
Add the AutoPostback="True" and OnSelectedIndexChanged="ddlConsole_SelectedIndexChanged" to the ddlConsole attributes. You can delete the OnInit method, since you bound the SeletedIndexChanged event at design time.

Nested ASP.NET DropDownList SelectedIndexChanged Not Firing

Have run this by my entire dev group to no avail. Seems simple enough, here's the question.
I have a UserControl with a single, non-databound dropdownlist. The UserControl is then dropped onto the masterpage and then also loaded into an inheriting webform that has the control manually loaded onto the page.
No AJAX, straight post-back.
The issue I am running into is that the SelectedIndexChanged event is not firing for the second instance of the DropDownList. The first one fires just fine.
TIA
Here is the code for the ascx:
<asp:dropdownlist id="SelectLanguage" autopostback="true" runat="server" enableviewstate="true">
<asp:listitem>- Select Language -</asp:listitem>
<asp:listitem value="xxx">Netherlands</asp:listitem>
<asp:listitem value="xxx">United Kingdom</asp:listitem>
<asp:listitem value="xxx">United States</asp:listitem>
</asp:dropdownlist>
Here is the CB for the user control:
protected override void OnInit(EventArgs e)
{
SelectLanguage.SelectedIndexChanged += new EventHandler(SelectLanguage_SelectedIndexChanged);
base.OnInit(e);
}
protected void SelectLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
// do something
}
Does the postback actually happen for the second dropdown, or does the page do nothing?
If the postback isn't happening, you have javascript errors on your page.
Not sure exactly how this is, but the FancyBox lightbox jQuery library we're using to do the lightbox effect around the 2nd drop down is somehow messing with the bindings on the back end. No real time to figure out why, just let it be known it does in case anyone else runs into this issue.

Categories

Resources