Question : ASP.NET change input value on button click from code behind

Hi,

I am trying to set up an asp.net ecommerce site to be useable without javascript enabled. Currently a javascript function fires when the user clicks the add to cart button which updates the value of a hidden field. This update inputs a new value which is the internal product reference and places the correct item in the users cart. We have the need to create the same functionality if the user has java disabled, we don't want anything more than a purely functional site without java and then if its enabled they can have all the bells and whistles.

The form, inputs and dropdown menus the user interacts with are all placed on the page by calls from an xml page to the codebehind. I have therefore just tried to add the following to the code:

buttonaddtocart.Click += new ImageClickEventHandler(text_update);

buttonaddtocart is an ImageButton and i hoped this would make the call to text_update upon the button click. The hidden input which passes the value of the selected product to the form is created as below:

HtmlGenericControl myHiddenField = new HtmlGenericControl("input");
myHiddenField.Attributes.Add("type", "hidden");
myHiddenField.Attributes.Add("name", "ProductID");
myHiddenField.ID = "ProductID_" + itemCounter;
myHiddenField.Attributes.Add("value", "" + itemCounter);

itemcounter contains the value of the overall product, this is the value that needs to be updated based on the selection the user makes. So to do this i wrote the text_update function i called:

public void text_update(object sender, EventArgs e)
        {
            int position = dropdown.SelectedIndex + 1;
           
            myHiddenField.Attributes.Clear();
            myHiddenField.Attributes.Add("type", "hidden");
            myHiddenField.Attributes.Add("name", "ProductID");
            myHiddenField.ID = "ProductID_" + position;
            myHiddenField.Attributes.Add("value", "" + position);
        }

this function is not complete as it currently doesn't recieve the value of itemcounter as i could not figure out how to pass this across to it. However i have tested it by hardcoding some values into it and i am finding it doesn't get called anyway.

i am hoping soemone can let me know what i am doing wrong. I will paste the relevant pieces  of code below. I have pasted all the code that is in the sections which means theres alot that isn't relevant, however i wasn't sure if it could be having an effect.

So to clarify the 2 things i need to do are:

- Get the call to text_update to work
- Pass the value of itemcounter to text_update so it can be used.

Any help would be greatly appreciated,

Guy
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
// Create dropdownlist for product attributes
        DropDownList dropdown = new DropDownList();
 
 
// Below region creates the dropdown menu which contains the 
// items variations for the user to chose from
 
        #region MatrixAttributesNew
 
        public virtual string MatrixAttributesNew(int itemCounter, string itemCode, string itemType, string align)
        {
 
            Panel DivHolder = new Panel();
            DivHolder.CssClass = "productchoice";
 
 
            StringBuilder output = new StringBuilder();
            
            string alignBreak = " ";
            if (align.ToLowerInvariant().Equals("vertical") || align.ToLowerInvariant().Equals("v"))
            {
                alignBreak = "
"; } Dictionary matrixAttributesByCode = new Dictionary(); using (IDataReader reader = DB.GetRS("exec aspdnsf_GetMatrixGroupAttributes @ItemCode = {0}", DB.SQuote(itemCode))) { while (reader.Read()) { string attributeCode = DB.RSField(reader, "AttributeCode"); string attributeDescription = DB.RSField(reader, "AttributeDescription"); string attributeValueCode = DB.RSField(reader, "AttributeValueCode"); string attributeValueDescription = DB.RSField(reader, "AttributeValueDescription"); MatrixAttribute currentAttribute = null; if (!matrixAttributesByCode.ContainsKey(attributeCode)) { currentAttribute = new MatrixAttribute(attributeCode); currentAttribute.AttributeCode = attributeCode; currentAttribute.AttributeDescription = attributeDescription; matrixAttributesByCode.Add(attributeCode, currentAttribute); } currentAttribute = matrixAttributesByCode[attributeCode]; currentAttribute.Values.Add(new MatrixAttributeValue(attributeValueCode, attributeValueDescription)); } } output.AppendFormat("
", itemCounter); output.AppendFormat("
", itemCounter); int ctr = 1; foreach (string attributeCodeAsKey in matrixAttributesByCode.Keys) { MatrixAttribute currentAttribute = matrixAttributesByCode[attributeCodeAsKey]; dropdown.ID = "MatrixAttribute_" + itemCounter + "_" + ctr; // select header ListItem list = new ListItem(); list.Value = currentAttribute.AttributeCode; list.Text = AppLogic.GetString("is_showproduct.aspx.54", ThisCustomer.SkinID, ThisCustomer.LocaleSetting) + " " + currentAttribute.AttributeDescription; dropdown.Items.Add(list); ListItemCollection listoptions = new ListItemCollection(); foreach (MatrixAttributeValue value in currentAttribute.Values) { ListItem listproducts = new ListItem(); listproducts.Value = Security.UrlEncode(value.AttributeValue); listproducts.Text = Security.HtmlEncode(value.AttributeValueDescription); listoptions.Add(listproducts); dropdown.Items.Add(listproducts); } DivHolder.Controls.Add(dropdown); ctr++; } StringWriter sw = new StringWriter(output); HtmlTextWriter writer = new HtmlTextWriter(sw); DivHolder.RenderControl(writer); return output.ToString(); } #endregion // this region creates the addtocart button and the hidden input which needs editing #region DisplayAddToCartFormNew HtmlGenericControl myHiddenField = new HtmlGenericControl("input"); public void text_update(object sender, EventArgs e) { int position = dropdown.SelectedIndex + 1; myHiddenField.Attributes.Clear(); myHiddenField.Attributes.Add("type", "hidden"); myHiddenField.Attributes.Add("name", "ProductID"); myHiddenField.ID = "ProductID_" + position; myHiddenField.Attributes.Add("value", "" + position); } public string DisplayAddToCartFormNew(int itemCounter, string itemCode, string itemType, string align) { ItemWebOption settings = ItemWebOption.GetWebOption(itemCode); //Check for wholesale parameter. StringBuilder output = new StringBuilder(); string alignBreak = " "; if (align.ToLowerInvariant().Equals("vertical") || align.ToLowerInvariant().Equals("v")) { alignBreak = "
"; } string action = "addtocart.aspx?returnurl=" + HttpContext.Current.Server.UrlEncode(CommonLogic.GetThisPageName(false) + "?" + CommonLogic.ServerVariables("QUERY_STRING")); output.AppendLine(); // by default let's make this invisible output.AppendFormat("
\n", itemCounter); output.AppendFormat("\n", itemCounter, action); // Should this be created using a HiddenField? If so how do you control the value? myHiddenField.Attributes.Add("type", "hidden"); myHiddenField.Attributes.Add("name", "ProductID"); myHiddenField.ID = "ProductID_" + itemCounter; myHiddenField.Attributes.Add("value", "" + itemCounter); StringWriter sw = new StringWriter(output); HtmlTextWriter writer = new HtmlTextWriter(sw); myHiddenField.RenderControl(writer); // output.AppendFormat("\n", itemCounter); output.AppendFormat("\n", itemCounter); output.AppendFormat("\n"); // output.Append(alignBreak); output.AppendFormat("
", itemCounter); output.AppendFormat("", itemCounter); output.AppendFormat("\n"); output.Append(alignBreak); output.AppendFormat("\n", itemCounter); output.AppendFormat("\n", itemCounter); output.Append("
\n"); Panel DivHolder = new Panel(); DivHolder.CssClass = "productchoice"; // output.Append(alignBreak); if (settings.ShowBuyButton) { string addToCartCaption = AppLogic.GetString("AppConfig.CartButtonPrompt", ThisCustomer.SkinID, ThisCustomer.LocaleSetting); ImageButton buttonaddtocart = new ImageButton(); buttonaddtocart.ID = "AddToCart_" + itemCounter; buttonaddtocart.ImageUrl = AppLogic.LocateImageURL("skins/skin_" + ThisCustomer.SkinID.ToString() + "/images/productpages/addtocart.jpg"); buttonaddtocart.AlternateText = "Add to Cart"; buttonaddtocart.Click += new ImageClickEventHandler(text_update); buttonaddtocart.OnClientClick += new ImageClickEventHandler(text_update); DivHolder.Controls.Add(buttonaddtocart); DivHolder.RenderControl(writer); } // output.Append(alignBreak); output.Append("\n"); output.Append("
\n"); return output.ToString(); } #endregion

Answer : ASP.NET change input value on button click from code behind

Sorry, i'm not sure i followed your last post. The values in the hidden fields above are filled in by the code when the page is loaded. these values then need to be changed dependant on the value of a dropdown selection.

the input image has a click event attached to it which doesn't update the ProductID hidden input even though in testing the function works (I'm not worried about updating the other hidden fields, i only need to change the value attribute within the ProductID hidden filed). When you attach a click event to an ImageButton does the click function fire before the form is submited?
Random Solutions  
 
programming4us programming4us