-
Need Urgent Help i cannot access value dynamic dropdownlist
Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class assigned : System.Web.UI.Page
{
DropDownList d;
DropDownList dr;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox4_TextChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(TextBox1.Text);
try
{
for (int i = 0; i < a; i++)
{
d = new DropDownList();
d.ID = "Text" + i;
LiteralControl l1 = new LiteralControl("<br></br>");
d.Items.Add("Rahul");
d.Items.Add("Gautum");
d.Items.Add("anant");
Panel3.Controls.Add(d);
Panel3.Controls.Add(l1);
}
}
catch (Exception e1)
{
Label4.Text = "" + e1.Message.ToString();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(TextBox1.Text);
string value = "";
for (int i = 0; i < a; i++)
{
string str ="Text" + i;
dr = new DropDownList();
dr = (DropDownList)(Panel3.FindControl(str));
value = dr.SelectedValue.ToString();
SqlConnection con = new SqlConnection("Data Source=ABC-0D78787890A;Initial Catalog=Amit;Integrated Security=True");
con.Open();
SqlCommand cmd1 = new SqlCommand("insert into niitstud values('" + value + "')", con);
cmd1.ExecuteNonQuery();
con.Close();
}
}
}
}
-
What errors are you getting?
-
Fix
Ok, the reason you are not getting info is that when you post back, the control does not exist. You need to rebuild the control again...
So, instead of building your control in the button event, try to put in your Page_Load a call to build your drop down list and add the content...
Then your button_2 will get the info...
I hope this makes sense....
Similar Threads
-
Replies: 2
Last Post: 04-08-2011, 11:40 PM
-
By wwwursa in forum Database
Replies: 1
Last Post: 03-07-2009, 02:20 PM
-
By prince123 in forum Database
Replies: 0
Last Post: 09-14-2005, 02:11 PM
-
By Todd B - Agendum Software in forum vb.announcements
Replies: 0
Last Post: 12-20-2000, 01:22 PM
-
By Marie in forum VB Classic
Replies: 0
Last Post: 11-20-2000, 06:32 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|