Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

hoping someone can point me in the right direction. I have some javascript dropdowns on a form, and want to pass the selected values of those ddl's to the server. so, I set a non-asp label to equal the values of both dropdowns. I now want to get that string, and pass it to the server. the problem is, anything on the client side will obviously not be "seen" server side, and that includes the code behind in my app.

 

here's the function code on the html view

function showItems()
{
var country = document.Form1.selCountry.value;
var province = document.Form1.selProv.value;

if ((country == '0') || (province == 'C0'))
	{
		alert('required! country and/or province');
	}
else
	{
		var myString = document.Form1.selCountry.value + ' ' + document.Form1.selProv.value;
		lblTest.innerText = myString;
		
	}
}

 

the label on the page will successfully show the values from the dropdown list, or select as javascript calls it, but I need that string to be passed to the server, ie, in my VB codebehind, I need to access the "text" of the label. how can I do this?

 

any assistance appreciated!

Posted

Usually I have a 'hidden' field that is a server control so that when it is updated the server knows about it. For the JavaScript portion, because I know you'll ask, I have an Init function that sets a variable that would be used in the function:

 

var myControl;

Init(serverControl)

{

myControl = document.getElementById(serverControl);

}

 

In a function like yours you would put:

 

myControl.value = document.Form1.selCoutnry.value + ' ' + ....etc....

 

Keep in mind though, that javascript you have will bomb on anything other than IE. You should always try to use document.getElementById since it's the 'most' cross-browser safe (from what I've seen).

Posted

hey, thanks for the reply! however, I'm not quite sure how I would use your example. by having a hidden field, will that still not be client side? example. In my vb codebehind, I did this

 

dim strTest as string = lblTest.InnerText.toString.

 

setting a breakpoint, I could not "see" the text in the label. I am guessing that when any postback occurs, the value is cleared from the control. Therefore, I was wondering if coding anything in the HTML will matter, as it still cannot be "seen" on the server side.

 

Hopefully, I am not being too dense, and as with all problems like this, it will most likely end up being a real forehead smacker once I see the light.

 

again, thanks for the help.

 

handleWithBeer.

Posted

If the original control is a server control then changes you make to it on the client will be seen on the server.

 

As an example, put a server label control on a form, type something into and then click a server 'LinkButton' control. In the OnClick event for the link button control look at the value of the server label control - it will have what you typed as the Text property value.

 

All I've done is moved that functionallity into a Hidden Control - drop a HiddenInputTag on the form, and then right-click on it and set it to be server control. If the value is changed on the client you will catch it in the ServerChange event.

Posted

Hope this helps

 

Step1. In your HTML

 

<form id="Form1" method="post" runat="server">

<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>

<SELECT id="Select1" runat="server">

<OPTION SELECTED>Item1</OPTION>

<OPTION>Item2</OPTION>

<OPTION>Item3</OPTION>

</SELECT>

</form>

 

Step2. In your Code behind

 

public class WebForm1 : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Button Button1;

protected System.Web.UI.HtmlControls.HtmlSelect Select1;

 

 

private void Button1_Click(object sender, System.EventArgs e) {

Response.Write(Select1.Value);

}

}

 

==================================

What you are looking at is:

==================================

1. Step1 make your HTML dropdown runat="server" and give it an ID (Select1 in this case)

 

2. Declare a variable with the same name as the ID and Type System.Web.UI.HtmlControls.HtmlSelect

 

Finally, access the HTML control as Select11.Value ! :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...