autopostback in a form submit [Resolved]

Posted by Jopito under ASP.NET on 12/5/2013 | Points: 10 | Views : 1129 | Status : [Member] | Replies : 13
Hi friends,have got a certain dropdown combobox which got a item studentId,am planning to use a postback on the control dropdown such that when that student Id is selected ,then the next textbox with a value age should be autofilled.What i need guidance is to someone explain me this techniqque correctly as how i can use the selected index changed sought of event for auto postback,on my part i was thinking to pass the query string,and then on page load thats where i would use that code.......

Thanks

Mark as answer if satisfied


Responses

Posted by: vishalneeraj-24503 on: 12/5/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Inside page-load event,you have to only fill your Student Dropdown-List and inside Student Dropdownlist-SelectedIndexChanged event,you have to write your code.

Suppose,you have dropdownlist named ddl_student
protected void ddl_student_SelectedIndexChanged()
{
if(ddl_student.Items.Count>0)
{
txt_age.Text = ddl_student.SelectedValue;

//here,SelectedValue will be your student age.
}
}

Please let me know.

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

Posted by: Allemahesh on: 12/5/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Dear Jopito,

Below is complete code for you requirement.

.aspx code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table width="50%" cellpadding="1" cellspacing="1">
<tr>
<td>
<asp:DropDownList ID="ddlStudent" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlStudent_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>


c# code:-

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadDroupDownList();
}
}

public void loadDroupDownList()
{
try
{
ddlStudent.Items.Clear();
for (int i = 1; i < 11; i++)
{
ddlStudent.Items.Add(new ListItem("Student " + i, Convert.ToString(i + 20)));
}
ddlStudent.Items.Insert(0, new ListItem("-- Select --", "-- Select --"));
}
catch
{
throw;
}
}

protected void ddlStudent_SelectedIndexChanged(object sender, EventArgs e)
{
txtAge.Text = string.Empty;
if (ddlStudent.SelectedIndex > 0)
{
txtAge.Text = Convert.ToString(ddlStudent.SelectedValue);
}
}
}


Please let me know if you have any issue.

If this helps you towards the solution, click on MARK IT AS ANSWER

Happy Coding.

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

Posted by: vishalneeraj-24503 on: 12/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Please take UpdatePanel and keep your Dropdownlist inside that update panel
as

<asp:UpdatePanel ID="updt" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>

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

Posted by: vishalneeraj-24503 on: 12/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
After,putting dropdownlist inside UpdatePanel,it will avoid postback,when you select dropdownlist value.

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

Posted by: vishalneeraj-24503 on: 12/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
You can also add Scriptmanager in your page as
<asp:ScriptManager ID="scm" runat="server"></asp:ScriptManager>
then you can add updatepanel.

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

Posted by: Jopito on: 12/5/2013 [Member] Starter | Points: 25

Up
0
Down
So let me get you right here,i mean when i select from dropdown the item like "Student2",in the textbox i need it to display the age of that selected item,,,

Mark as answer if satisfied

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

Posted by: vishalneeraj-24503 on: 12/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Then you need to bind dropdown list as
select student_name,age from table_name;

here age will be your selected value
when you select student from dropdownlist then write in ddl_selectedindexchanged event
as
text_age.Text = ddl.SelectedValue.

Then selected student age will be assigned to textbox.
Here selectedvalue will be your age.



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

Posted by: Bandi on: 12/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer
http://asp-net-example.blogspot.in/2009/03/how-to-use-dropdownlist-autopostback.html

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

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

Posted by: vishalneeraj-24503 on: 12/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi Jopito,
Did you find solution or not?

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

Posted by: Jopito on: 12/5/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Vishal,av not managed am still checking for the links.am cross checkin the links

Mark as answer if satisfied

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

Posted by: Jopito on: 12/5/2013 [Member] Starter | Points: 25

Up
0
Down
I gotta try this ones really....lemme work them out al b back for feedback



Mark as answer if satisfied

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

Posted by: Allemahesh on: 12/6/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Jopito,

Have you solved your problem?

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

Posted by: Jopito on: 12/6/2013 [Member] Starter | Points: 25

Up
0
Down
hi ,sorry for late reply my pc had issues.Have tried it now and it returns the values well.Thanks my friends

Mark as answer if satisfied

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

Login to post response