Postback and Javascript issue

Posted by Laghaterohan under ASP.NET on 5/11/2011 | Points: 10 | Views : 2760 | Status : [Member] | Replies : 6
Hello,
I have a drop down list in which i have one option as Others...on the section of this text i make one text box visible true below it by using javascript. However, above the drop down list i have a text box and a text change event associated with it so in such scenario my textbox that i generated dynamically disappears....due to postback event is fired on text change event....How to overcome this problem??

Please help....:)


Thanks!

Best Regards,
Rohan Laghate



Responses

Posted by: Lokesh76 on: 5/11/2011 [Member] Starter | Points: 25

Up
0
Down
When you make you textbox visible false in javascript that time put flag Y into a hidden field and after any postback happen check the value of hidden field in code behind if its Y then make you textbox visible after postback too.

Thanks
Lokesh Kumar

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Laghaterohan on: 5/11/2011 [Member] Starter | Points: 25

Up
0
Down
can u give example? and also the js is lost on postback...how can we do it?

Best Regards,
Rohan Laghate

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lokesh76 on: 5/11/2011 [Member] Starter | Points: 25

Up
0
Down
Please find attached sample code.
 Download source file

Thanks
Lokesh Kumar

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 5/12/2011 [Member] Bronze | Points: 25

Up
0
Down
pls find the below example...

aspx:
<body> 

<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt1" runat="server" ontextchanged="txt1_TextChanged" AutoPostBack="true" /><br/><br />
<asp:DropDownList ID="ddl1" runat="server" onChange="return fun();">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>others</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txt2" runat="server" />
</div>
</form>
</body>


aspx.cs
public partial class textbox : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
txt2.Attributes["style"] = "display:none;";
else
if(ddl1.SelectedItem.Text=="others")
txt2.Attributes["style"] = "display:block;";
}

protected void txt1_TextChanged(object sender, EventArgs e)
{
txt1.Text = "Nitha";
}
}


javascript
function fun() {

var n = document.getElementById('ddl1').value;
if(n=="others")
document.getElementById('txt2').style.display="block";
}


If this code helps you ,mark it as answer....

Nitha Deepak

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lokesh76 on: 5/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Deepak,

I just go through your code looks good but Its not good to make AutoPostBack="true" for dropdownlist its cause to submit your page every time you change value. go through my attached source too.

Thanks
Lokesh Kumar

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 5/12/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi Lokesh,

Yes,No need to make AutoPostBack="true" for dropdownlist.Thanks for the comment. I just updated the code by removing the AutoPostBack property of dropdownlist.

Thanks,

Nitha Deepak

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response