I have a small web form that is used to archive jobs. The user puts in the job number in a textbox and then a dropdown needs to be filled with the customers that are bidding the job. The user also chooses whether the job was won or lost or a duplicated. Then they submit the form and the sql database is updated. I can't find and examples of populating a dropdown list based on a textbox. All the examples I find are populating textboxes based on the dropdown.
<div class="container">
<h2>Job Archive Form</h2>
<div class="form-group">
<asp:Label ID="lblJobNumber" runat="server">Job Number</asp:Label><br />
<asp:TextBox ID="archiveJobNumber" runat="server" AutoPostBack="True" OnTextChanged="archiveJobNumber_TextChanged"/>
</div>
<div class="form-group">
<asp:Label ID="lblJobStatus" runat="server">Job Status</asp:Label><br />
<asp:DropDownList ID="archiveJobStatus" runat="server">
<asp:ListItem value="">--Select--</asp:ListItem>
<asp:ListItem value="1">Active</asp:ListItem>
<asp:ListItem value="2">Won</asp:ListItem>
<asp:ListItem value="3">Lose</asp:ListItem>
<asp:ListItem value="4">Duplicate</asp:ListItem>
</asp:DropDownList>
</div>
<div class="form-group">
<asp:Label ID="lblCustomer" runat="server">Customer</asp:Label><br />
<asp:DropDownList ID="archiveCustomer" runat="server" DataSourceID="archiveSqlDSCustomer" DataTextField="Name" DataValueField="CustID" AutoPostBack="True"></asp:DropDownList>
</div>
<asp:Button ID="submit" runat="server" Text="Submit" OnClick="submit_Click"></asp:Button>
</div>
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
namespace JobsApp
{
public partial class Archive : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UID"] == null)
Response.Redirect(@"Account\Login.aspx");
}
protected void submit_Click(object sender, EventArgs e)
{
string constring = "";
constring = ConfigurationManager.ConnectionStrings["JobsConstr"].ToString();
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand cmd = new SqlCommand("usp_changeJobStanding", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter jobNumber = cmd.Parameters.Add("@jobNumber", SqlDbType.VarChar);
SqlParameter jobStanding = cmd.Parameters.Add("@jobStanding", SqlDbType.Int);
jobNumber.Value = archiveJobNumber.Text;
jobStanding.Value = archiveJobStatus.Text;
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("~/Archive.aspx");
}
protected void archiveJobNumber_TextChanged(object sender, EventArgs e)
{
archiveSqlDSCustomer.SelectCommand = "GetCustomerListByJobNumber";
archiveCustomer.DataSourceID = "archiveSqlDSCustomer";
}
}
}
You could retrieve data from database inside archiveJobNumber_TextChanged
event, and bind the data to DropDownList
.
For example,
<asp:TextBox ID="archiveJobNumber" runat="server" AutoPostBack="True"
OnTextChanged="archiveJobNumber_TextChanged" />
<asp:DropDownList ID="archiveCustomer" runat="server"
DataTextField="Name" DataValueField="CustID" AutoPostBack="True">
</asp:DropDownList>
protected void archiveJobNumber_TextChanged(object sender, EventArgs e)
{
var query = "SELECT CustID, Name FROM Users WHERE CustID= @Id";
using (var con = new SqlConnection(connectionString))
using (var da = new SqlDataAdapter(query, con))
{
con.Open();
var dt = new DataTable();
da.SelectCommand.Parameters.AddWithValue("@Id", archiveJobNumber.Text);
da.Fill(dt);
archiveCustomer.DataSource = dt;
archiveCustomer.DataBind();
}
}
Please make sure query is correct.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.