Hi there,
How do I add an item when I'm already on Form.aspx.cs?
cboOriginalBrand.Items.Add("*"); doesn't work anymore.
Hello Andrea ,
Thank you for posting in our forum.
Do you mean when the page is already loaded in the browser?
If so you can add it on the client side using java script:
http://ko.infragistics.com/community/forums/p/78574/398781.aspx#398781
Let me know if you perhaps meant something different.
Best Regards,
Maya Kirova
Developer Support Engineer II
Infragistics, Inc.
http://ko.infragistics.com/support
Thank you or the clarification.
The WebDropDown’s Items Add() method accepts objects of type DropDownItem. So you would need to create a new DropDownItem with some text and add it to the Items collection. For example:
WebDropDown1.Items.Add(new DropDownItem() { Text = "Some Text" });
Let me know if you have any questions.
Hi Maya,
This is the code I currently have:
protected void setCombo(){string strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ORIGSAMPLE"].ToString();
SqlConnection cn = new SqlConnection(strConn);
try{cn.Open();
string strSQL = "SELECT * FROM [qry_Original_Brands] ORDER BY 1";
SqlCommand cmd = new SqlCommand(strSQL, cn);SqlDataReader dr = cmd.ExecuteReader();
cboOriginalBrand.Items.Add("*");
if (dr.HasRows){while (dr.Read()){cboOriginalBrand.Items.Add(dr[0].ToString());}}
I'm having a problem with cboOriginalBrand.Items.Add("*"); and cboOriginalBrand.Items.Add(dr[0].ToString());
Please help me in this.
Make sure you have added the following reference:
using Infragistics.Web.UI.ListControls;
Since the DropDownItem class is defined in that namespace.
I’ve attached an example for your reference. Let me know if you have any questions.
Adding using Infragistics.Web.UI.ListControls solved it! :D However, how do I fix the other one? cboProductGroup.Items.Add(dr[0].ToString());
As I said the Add() method does not accept string.
It accepts a DropDownItem object.
So you need to create a new DropDownItem and set it’s Text. The Text will be the string that will be displayed in the item.
For example:
cboProductGroup.Items.Add(new DropDownItem() { Text = dr[0].ToString()});
Let me know if you have any further questions.
I’m just following up to see if you’ve been able to resolve your issue. If you have any questions or concerns or if you need further assistance please let me know.
Developer Support Engineer
Hello Andrea,
I suggest you debug your code and see which element is null on this line:
string strProductType = cboProductType.CurrentValue.Replace("*", "%");
If cboProductType.CurrentValue returns null, then there’s no value entered for the WebDropDown and the CurrentValue returning null is expected.
In that case you can check if the value is null and it it’s null just set it to an empty string :
string strProductType = "";
if (cboProductType.CurrentValue !=null)
{
strProductType = cboProductType.CurrentValue.Replace("*", "%");
}
This is my whole code for the program/form:
using System;using System.Collections.Generic;using Infragistics.Web.UI.ListControls; using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;using System.Data;
public partial class OriginalSampleList : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { //if (Session["USERNAME"] == null) //{ // Response.Redirect("~/Login.aspx"); //}
if (Session["USERNAME"] == null) {
HttpCookie cookie = Request.Cookies["WEBPLM_USERNAME"];
if (cookie != null) {
Session["USERNAME"] = cookie.Value.ToString(); } }
if (!Page.IsPostBack) { setCombo(); }
if (Session["SQL"] != null) { sdsOriginalSamples.SelectCommand = Session["SQL"].ToString(); } grdOriginalSamples.DataBind(); }
protected void setCombo() { string strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ORIGSAMPLE"].ToString();
try { cn.Open();
SqlCommand cmd = new SqlCommand(strSQL, cn); SqlDataReader dr = cmd.ExecuteReader();
cboOriginalBrand.Items.Add(new DropDownItem() { Text = "*" });
if (dr.HasRows) { while (dr.Read()) {
cboOriginalBrand.Items.Add(new DropDownItem() { Text = dr[0].ToString() });
} }
cboOriginalBrand.NullText = "*";
dr.Close();
strSQL = "SELECT DISTINCT [Description] FROM [qry_Product_Groups] ORDER BY 1";
cmd.CommandText = strSQL; dr = cmd.ExecuteReader();
cboProductGroup.Items.Add(new DropDownItem() { Text = "*" });
if (dr.HasRows) { while (dr.Read()) { //cboProductGroup.Items.Add(dr[0].ToString()); cboProductGroup.Items.Add(new DropDownItem() { Text = dr[0].ToString() }); } }
cboProductGroup.NullText = "*";
strSQL = "SELECT * FROM [ProductType] ORDER BY 1";
cboProductType.Items.Add(new DropDownItem() { Text = "*" });
if (dr.HasRows) { while (dr.Read()) { //cboProductType.Items.Add(dr[0].ToString()); cboProductType.Items.Add(new DropDownItem() { Text = dr[0].ToString() }); } }
cboProductType.NullText = "*";
strSQL = "SELECT DISTINCT [Gender] FROM [Gender] ORDER BY 1";
cboGender.Items.Add(new DropDownItem() { Text = "*" });
if (dr.HasRows) { while (dr.Read()) { //cboGender.Items.Add(dr[0].ToString()); cboGender.Items.Add(new DropDownItem() { Text = dr[0].ToString() }); } }
cboGender.NullText = "*";
strSQL = "SELECT * FROM [qry_Designers] ORDER BY 1";
cboDesigner.Items.Add(new DropDownItem() { Text = "*" });
if (dr.HasRows) { while (dr.Read()) { //cboDesigner.Items.Add(dr[0].ToString()); cboDesigner.Items.Add(new DropDownItem() { Text = dr[0].ToString() }); } }
cboDesigner.NullText = "*";
strSQL = "SELECT * FROM [qry_Buyers] ORDER BY 1";
cboBuyer.Items.Add(new DropDownItem() { Text = "*" });
if (dr.HasRows) { while (dr.Read()) { //cboBuyer.Items.Add(dr[0].ToString()); cboBuyer.Items.Add(new DropDownItem() { Text = dr[0].ToString() }); } }
cboBuyer.NullText = "*";
strSQL = "SELECT * FROM [Borrowers] ORDER BY 1";
cboBorrower.Items.Add(new DropDownItem() { Text = "*" });
if (dr.HasRows) { while (dr.Read()) { //cboBorrower.Items.Add(dr[0].ToString()); cboBorrower.Items.Add(new DropDownItem() { Text = dr[0].ToString() }); } }
cboBorrower.NullText = "*";
dr.Close(); dr.Dispose();
strSQL = "SELECT * FROM [qry_Categories] ORDER BY 1";
cboCategory.NullText = "*";
if (dr.HasRows) { while (dr.Read()) { //cboCategory.Items.Add(dr[0].ToString()); cboCategory.Items.Add(new DropDownItem() { Text = dr[0].ToString() }); } }
cn.Close(); cn.Dispose(); } catch (Exception ex) { Response.Write(ex.Message); } }
protected void grdOriginalSamples_SelectedIndexChanged(object sender, EventArgs e) { //Response.Redirect("~/OriginalSampleForm.aspx?ORIGINALSAMPLENO=" + }
protected void grdOriginalSamples_RowDataBound(object sender, GridViewRowEventArgs e) {
//Response.Write(System.Web.VirtualPathUtility.GetDirectory());
string strHTTPLocal = GlobalVars.THUMBNAIL_PATH;
if (e.Row.RowType == DataControlRowType.DataRow) { try { Image imgFront = e.Row.FindControl("imgFront") as Image; Image imgBack = e.Row.FindControl("imgBack") as Image; Label lblOriginalSampleNo = e.Row.FindControl("lblOriginalSampleNo") as Label; HyperLink hlpSelect = e.Row.FindControl("hplSelect") as HyperLink;
hlpSelect.NavigateUrl = "~/OriginalSampleForm2.aspx?ORIGINALSAMPLENO=" + lblOriginalSampleNo.Text;
if (imgFront.ImageUrl == null || imgFront.ImageUrl == "") { imgFront.ImageUrl = "XXX"; } if (imgBack.ImageUrl == null || imgBack.ImageUrl == "") { imgBack.ImageUrl = "XXX"; }
imgFront.ImageUrl = strHTTPLocal + "?filename=" + imgFront.ImageUrl + @"&NEWWIDTH=185&MAXHEIGHT=215&ONLYRESIZEIFWIDER=true&USEDPICONVERT=false"; imgBack.ImageUrl = strHTTPLocal + "?filename=" + imgBack.ImageUrl + @"&NEWWIDTH=185&MAXHEIGHT=215&ONLYRESIZEIFWIDER=true&USEDPICONVERT=false"; } catch (Exception ex) { Response.Write(ex.Message); } } } protected void btnApply_Click(object sender, EventArgs e) { string strProductType = cboProductType.CurrentValue.Replace("*", "%"); string strProductGroup = cboProductGroup.CurrentValue.Replace("*", "%"); string strGender = cboGender.CurrentValue.Replace("*", "%");
string strBatchNumber = txtBatchNo.Text; if (txtBatchNo.Text == "") { strBatchNumber = "*"; } strBatchNumber = strBatchNumber.Replace("*", "%"); string strDesigner = cboDesigner.CurrentValue.Replace("*", "%"); string strSource = cboSource.CurrentValue.Replace("*", "%"); string strBuyer = cboBuyer.CurrentValue.Replace("*", "%"); string strBorrower = cboBorrower.CurrentValue.Replace("*", "%"); string strOriginalBrand = cboOriginalBrand.CurrentValue.Replace("*", "%"); string strCategory = cboCategory.CurrentValue.Replace("*", "");
string strSQL = "SELECT o.* FROM qry_Styles AS o " + " LEFT JOIN (" + "SELECT [Main Key],MAX([Borrower]) AS [Borrower] FROM [Library Log] WHERE [Date Borrowed] IS NOT NULL AND [Date Returned] IS NULL" + " GROUP BY [Main Key] " + ") As l" + " ON o.[Original Sample Number] = l.[Main Key] " + " WHERE o.[Original Sample Number] LIKE '" + txtOriginalSampleNo.Text.Trim().ToUpper() + "%'" + " AND ISNULL(o.[Product Group],'') LIKE '" + strProductGroup + "'" + " AND ISNULL(o.[Product Type],'') LIKE '" + strProductType + "'" + " AND ISNULL(o.[Gender],'') LIKE '" + strGender + "'" + " AND ISNULL(o.[Batch Number],'') LIKE '" + strBatchNumber + "'" + " AND ISNULL(o.[Designer],'') LIKE '" + strDesigner + "'" + " AND ISNULL(o.[Sample Type],'') LIKE '" + strSource + "'" + " AND ISNULL(o.[Buyer],'') LIKE '" + strBuyer + "'" + " AND ISNULL(l.[Borrower],'') LIKE '" + strBorrower + "'" + " AND ISNULL(o.[Original Brand], '') LIKE '" + strOriginalBrand + "'";
if (strCategory != "") strSQL = strSQL + " AND ISNULL(o.[Category],'') LIKE '" + strCategory + "'";
strSQL = strSQL + " ORDER BY o.[Original Sample Number]"; Session["SQL"] = strSQL; sdsOriginalSamples.SelectCommand = strSQL; grdOriginalSamples.DataBind();
} protected void btnCreateNewRecord_Click(object sender, EventArgs e) { Response.Redirect("~/OriginalSampleForm2.aspx"); } protected void btnReset_Click(object sender, EventArgs e) { txtBatchNo.Text = "*"; txtOriginalSampleNo.Text = ""; cboDesigner.ClearSelection(); cboGender.ClearSelection(); cboOriginalBrand.ClearSelection(); cboProductGroup.ClearSelection(); cboProductType.ClearSelection(); txtOriginalSampleNo.Focus(); }}
I’ve tested this on my side and it works as expected.
Please feel free to refer to the attached sample and test it on your side.
If you have any questions let me know.