Linking database column to dropdown control in ASP.Net - asp.net

I am using c# with asp.net and SQL Server 2005 as backend. I want to use dropdown list control in a form to populate a textbox. The dropdown control is linked to a column in the database. How can I code this in c#?

Do you mean this
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.datasource = list;
DropDownList1.datBind();
}
protected void DropDownList1_OnSelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = DropDownList1.SelectedValue;
}
Or this
protected void DropDownList1_OnSelectedIndexChanged(object sender, EventArgs e)
{
TextBox t = new TextBox();
t.Text = DropDownList1.SelectedValue;
body.InnerHtml.add(t);
}

Related

asp.net: Create Handles clause for control class

Is there a way that I force all TextBoxes (as an example) .. on an asp.net form, to go through the same routine, during a "postback" event or onchange event?
Here is example code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
textBox1.TextChanged += new EventHandler(textBox_TextChanged);
textBox2.TextChanged += new EventHandler(textBox_TextChanged);
}
}
protected void textBox_TextChanged(object sender, EventArgs e)
{
}

How to Bind Data Source to Button Click Event in ASP.net

How do i bind a datasource to a button ,when i run this the grid view does not populate.
C# CODE
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
You're setting the wrong property.
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.DataSourceObject = SqlDataSource1;
GridView1.DataBind();
}

GridView | drop down lists gets unpopulate

The Drop Down Lists in the FooterTemplate gets unpopulate when clicking "update" on some row
this is the page load event when they gets populate:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddlImages_new = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddlImages_new"));
ddlImages_new.DataSource = GetPdfs();
ddlImages_new.DataBind();
DropDownList ddl_invNamesNew = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddl_invNamesNew"));
ddl_invNamesNew.DataSource = GetInvestigatorNames();
ddl_invNamesNew.DataBind();
}
If I click the update linkButton on some row the data in the drop down lists are disappear
Even when try to call the page load on cancel event it didn't work.
protected void gvAdminArticleAdd_CancelEditEventHandler(object sender, GridViewCancelEditEventArgs e)
{
Page_Load(sender, e);
}
Bind your controls only when the page is not post back:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList ddlImages_new = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddlImages_new"));
ddlImages_new.DataSource = GetPdfs();
ddlImages_new.DataBind();
DropDownList ddl_invNamesNew = ((DropDownList)gvAdminArticleAdd.FooterRow.FindControl("ddl_invNamesNew"));
ddl_invNamesNew.DataSource = GetInvestigatorNames();
ddl_invNamesNew.DataBind();
}
}

GridView Editing when bound to a Datatable (no database)

I am a beginner at ASP.NET and I have been trying to allow editing/updating of a GridView that is bound to a Datatable, without apparent success.
The GridView, named "WeightGridView", has two columns, "Asset Class" and "Weight", and is populated upon the click of a button "AddAssetsButton". Once you click on this button, the GridView will be populated with the values of a listbox, "ListBox2". The code for this event handler is:
protected void AddAssetsButton_Click(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Asset Class", typeof(string));
table.Columns.Add("Weight", typeof(double));
foreach (ListItem li in ListBox2.Items)
{
DataRow dr = table.NewRow();
dr["Asset Class"] = li.Value;
dr["Weight"] = 0;
table.Rows.Add(dr);
}
WeightGridView.DataSource = table;
WeightGridView.DataBind();
SelectAssetsButton_ModalPopupExtender.Hide();
}
I have created a RowEditing and RowUpdating functions for the GridView, but they don't seem to work properly.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
WeightGridView.DataBind();
}
}
protected void WeightGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
WeightGridView.EditIndex = e.NewEditIndex;
}
protected void WeightGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string weight;
weight = ((TextBox)WeightGridView.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
WeightGridView.EditIndex = -1;
WeightGridView.DataBind();
}
Should you need any additional code to answer this post, please let me know and I will update the post.
Any help will be appreciated! Thank you!

problem in updating the textbox data into SQLSERVER

I have problem in updating user profile through asp:TextBox.
At the time of page load , all the textboxes get loaded with correct values from the database sqlserver.
say First name=SAM {i.e SAM is shown in txtName's textfield)
and Last Name=Berton
but when i alter the content of txtName to SAMANTHA
and similary for other textboxes, and click on update profile button, Internally txtName.text="SAM" gets retained notwithstanding the changed content of txtName.text="SAMANTHA" by manually altering the content of textbox.So same value that is "SAM" gets stored into the sqlserver rather than "SAMANTHA".
protected void Page_Load(object sender, EventArgs e)
{
loadProfileData();
}
protected void loadProfileData()
{
string connStringProfileload = ConfigurationManager.ConnectionStrings["myconnString"].ConnectionString;
SqlConnection conProfLoad = new SqlConnection(connStringProfileload);
conProfLoad.Open();
string emailAddLogin = User.Identity.Name.ToString();
string strSqlProfileLoad = "SELECT * FROM [gen_profile] WHERE [email]=#email";
SqlCommand cmd = new SqlCommand(strSqlProfileLoad, conProfLoad);
cmd.Parameters.AddWithValue("#email", emailAddLogin);
SqlDataReader drProfileLoad = cmd.ExecuteReader();
while (drProfileLoad.Read())
{
txtName.Text = drProfileLoad["fname"].ToString();
txtLastName.Text=drProfileLoad["lname"].ToString();
}
drProfileLoad.Close();
conProfLoad.Close();
}
protected void BtnUpdtProf_Click(object sender, EventArgs e)
{
string connStringProfileUpdate = ConfigurationManager.ConnectionStrings["myconnString"].ConnectionString;
SqlConnection conUpdateProf = new SqlConnection(connStringProfileUpdate);
conUpdateProf.Open();
string emailAddLogin = User.Identity.Name.ToString();
string strSqlUpdateProf = "UPDATE gen_profile SET fname =#fname, lname =#lname where email=#email;";
SqlCommand cmdUpdate = new SqlCommand(strSqlUpdateProf, conUpdateProf);
cmdUpdate.Parameters.AddWithValue("#fname", txtName.Text.ToUpper().Trim());
cmdUpdate.Parameters.AddWithValue("#lname", txtLastName.Text.ToUpper().Trim());
cmdUpdate.Parameters.AddWithValue("#email", emailAddLogin);
int i = cmdUpdate.ExecuteNonQuery();
conUpdateProf.Close();
if (i == 1)
Response.Redirect("~/prof/resp_update_prof.aspx");
}
Change this method :
protected void Page_Load(object sender, EventArgs e) {
loadProfileData();
}
to :
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) loadProfileData();
}
Basically, your textBox values are being reset in the page load event, so everytime your page posts back, it's resetting the values in the textboxes and then saving them back to the database.
This is because before your button click event, page_load get's fired which re-populates the textbox.
You can stop this happening by checking for a post back as follows:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack)
{
loadProfileData();
}
}

Resources