Adding Items to ListBox Control

Self-Innovator
Posted by Self-Innovator under ASP.NET category on | Points: 40 | Views : 1102
Page Design .aspx
<table align="center">
<tr>
<td>
Enter Customer Id
</td>
<td>
<asp:TextBox ID="txtCustomerID" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server"
ControlToValidate="txtCustomerID" ValidationGroup="v1"
ErrorMessage="*" Width="53px"></asp:RequiredFieldValidator>
<cc1:AutoCompleteExtender ID="txtCustomerID_AutoCompleteExtender"
runat="server" DelimiterCharacters="" Enabled="True" ServicePath="~/Admin/AutoComplete.asmx" ServiceMethod="GetSerialNumber"
MinimumPrefixLength="1" CompletionInterval="3" EnableCaching="true" CompletionSetCount="12"
TargetControlID="txtCustomerID">
</cc1:AutoCompleteExtender>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnAddtoList" runat="server" Text="Add to list" class="mybutton"
onclick="btnAddtoList_Click" ValidationGroup="v1" />
<asp:Button ID="btnRemoveFromList" runat="server" Text="Remove from List" class="mybutton"
onclick="btnRemoveFromList_Click" />
</td>
</tr>
<tr>
<td></td>
<td>
<asp:ListBox ID="lstUsers" runat="server"></asp:ListBox>
</td>
</tr>
</table>

Add Items Event
protected void btnAddtoList_Click(object sender, EventArgs e)
{
System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem(txtCustomerID.Text);
string Serial = txtCustomerID.Text;
dt = ee.GetUserID(Serial);
if (dt.Rows.Count == 0)
{
txtCustomerID.Focus();
txtCustomerID.Text = "";
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Invalid Serial No');", true);
}
else
{
if (!lstUsers.Items.Contains(li))
{
lstUsers.Items.Add(li);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Customer Id already exists');", true);
}
}
txtCustomerID.Text = "";
}

Method
 public DataTable GetUserID(string Serial)
{
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("sp_getuserid", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@serial", Serial);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ada.Fill(dt);
return dt;
}

Ajax Auto Complete Method
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {

public AutoComplete () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string[] GetSerialNumber(string prefixText, int count)
{
if (count == 0)
{
count = 10;
}
DataTable dt = GetRecords(prefixText);
List<string> items = new List<string>(count);
for (int i = 0; i < dt.Rows.Count; i++)
{
string strName = dt.Rows[i][0].ToString();
items.Add(strName);
}
return items.ToArray();
}
public DataTable GetRecords(string strName)
{
string strConn = ConfigurationManager.ConnectionStrings["cons"].ConnectionString;
SqlConnection con = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.AddWithValue("@Name", strName);
cmd.CommandText = "Select propertyvaluesstring from dbo.aspnet_Profile where propertyvaluesstring like '%'+@Name+'%'";
DataSet objDs = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
con.Open();
dAdapter.Fill(objDs);
con.Close();

DataTable dt = new DataTable();
dt.Columns.Add("serialno");
for (int i = 0; i < objDs.Tables[0].Rows.Count; i++)
{
string temp = objDs.Tables[0].Rows[i]["propertyvaluesstring"].ToString();
string[] temp1 = temp.Split('M');
dt.Rows.Add("M"+temp1[1].ToString());

}
return dt;
}
}

Delete from List
 protected void btnRemoveFromList_Click(object sender, EventArgs e)
{
if (lstUsers.Items.Count <= 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('List is empty');", true);
}
if (lstUsers.SelectedIndex == -1)
{
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Select the users to remove');", true);
}
else
{
lstUsers.Items.Remove(lstUsers.SelectedItem.Text);
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Customer Id removed successfully');", true);
}
}

Comments or Responses

Login to post response