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
Related
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");
}
}
I'm going crazy trying to send the value of an HTML text input (without runat="server") from a user control to a code behind. I get an empty string with no error. The steps are as follows:
User clicks on the ImageButton with ID="name_btn".
The value must be passed to the code behind. I'm testing to see if the value is passed by using the page load (if (IsPostBack)) and the button click function but still NO VALUE in both.
Note that for some other reason, I don't want the text input to run on the server so I don't want to add runat="server". Any help please?
User Control page (name.ascx)
<input type="text" ID="name" />
<asp:ImageButton runat="server" ID="name_btn" OnClick="name_Click" ImageUrl="~/icon-ok.png" />
User control - Code behind (name.ascx.cs)
public string nameBox;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
nameBox = Request.Form["name"];
Response.Write("Name: " + nameBox);
}
}
protected void name_Click(object sender, EventArgs e)
{
nameBox = Request.Form["name"];
Response.Write("Name: " + nameBox);
}
Master Page
<uc:name runat="server" ID="UCname" />
I would imagine that there is a bit of a conflict with what you are trying to do. For example, on the server you are using Request.Form[""] to get the html input element but you are requesting it by the id attribute. Request.Form supports finding elements by index and name.
So have you tried adding a name="" to the html input element?
you should add a name attribute to your input tag, example:
<input type="text" id="name" name="name"/>
then you can use:
if (IsPostBack)
{
nameBox = Request.Form["name"];
Response.Write("Name: " + nameBox);
}
The Request.Form(element)[(index)|.Count] Parameters:
element
The name of the form element from which the collection is to retrieve values.
index
An optional parameter that enables you to access one of multiple values for a parameter. It can be any integer in the range 1 to Request.Form(parameter).Count.
You can pass the value to codebehind using a javascript in two ways.
1. Onclick of your image button call a javascript.
Set your textbox value in __EVENTARGUMENT and call __doPostback(). And in pageload you can check the value in __EVENTARGUMENT.
From the javascript you can directly call a Function in your server using PageMethods. Here your function must be a WebMethod.
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.
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
Is there any way to directly access a field of class in the aspx page?
I have tried this:
In the aspx page I have added:
<form id="LoginForm" runat="server">
<asp:Label Text='<%# Eval("Test") %>' runat="server" ID="jym" />
</form>
and in the backend class of this page I have declared a property as:
private string test;
public string Test {
get {
return test;
}
set {
test = value;
}
}
This property is initialized in Page_Load() as: Test = "JYM";
But the problem is I am unable to see this value in the browser. The tag is rendered into <span/> but without any content.
What I am doing wrong?
Are you calling Page.DataBind() in Page_Load()?
You are using the # data binding operator in your embedded code block. The values that you bind to the control will only show after you have called DataBind() from either the control or the page (which will in turn call it for each control on the page).
If you only want to bind a value to that single label control you could just call jym.DataBind().
I would however suggest using a more descriptive name (id) for the Label control so it is clearer in the code about what is being databound.
See this page for more details.
UPDATE (extra info requested by OP in comment)
So you have 3 options:
1.
Call DataBind() as suggested above.
2.
Don't use a server side control for the label. Just use plain HTML and then you can use the following syntax:
<p><%= Test %></p>
3.
Set the value of the label in the code behind. For example in your page load you might have the following:
protected void Page_Load(object sender, EventArgs e)
{
jym.Text = Test;
}
You may want to use <%= this.Test%>. You can also do this.jym.Text=Test; in the Page Load evt.