Clicky

I need to reset certian (not all) asp.net input names in an ASP.NET Templet field.
I know how to reset textboxes, radio buttons and checkboxes I need to know how to reset this:
                                 <asp:TemplateField HeaderText="Price Per Unit" ItemStyle-CssClass="gvCust cellSize80" SortExpression="priceperunit">                                     <ItemTemplate>                                         <input name="inPricePerUnit" value="<%# Eval("priceperunit", "{0:#,##0.#0}") %>" class="editFieldText75" onfocus="inPricePerUnit_OnData(<%= RowId() %>, this);" onkeypress="return noReturn(event);" />                                     </ItemTemplate>                                 </asp:TemplateField>                             
1: 2: 3: 4: 5: 

Select allOpen in new window


Were a value is either typed into or already populated
Any thoughts.....and help will be greatly appricated

asked 12/12/2011 01:57

newjeep19's gravatar image

newjeep19 ♦♦


18 Answers:
To the Input control add to more attributes as

runat="server" type=""

<input name="inPricePerUnit" value="<%# Eval("priceperunit", "{0:#,##0.#0}") %>" class="editFieldText75" onfocus="inPricePerUnit_OnData(<%= RowId() %>, this);" onkeypress="return noReturn(event);" runat="server" type=""/>

Add the Using statement for HTML controls
using System.Web.UI.HtmlControls

Then you can do as you want
HTMLInput cntrlInut = gv.//Logic used for other controls
link

answered

santhimurthyd's gravatar image

santhimurthyd

Thanks for your reply :
However, i get this error message:
Error:
"Server Tag is not well formed."

And I need to reset certian fields but not all. I have this C# code that sill reset <asp.Textbox> and <asp:CheckBox> but i need to rest <intem name>
C# code:
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:
private void ResetFormControlValues(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.Controls.Count > 0)
                {
                    // ResetFormControlValues(c);
                    //txt1.Text = string.Empty;
                    //txt3.Text = string.Empty;
                    //txt4.Text = string.Empty;
                   // TextBox inListPrice = (TextBox(string.Empty));
                    TextBox priceText = (TextBox)parent.Item.Cells[3].Controls[0];

                    
                }
                else
                {
                    switch (c.GetType().ToString())
                    {
                        case "System.Web.UI.WebControls.TextBox": ((TextBox)c).Text = "";
                            break;
                        case "System.Web.UI.WebControls.CheckBox":
                            ((CheckBox)c).Checked = false;
                            break;
                    }
                }
            }
        }
link

answered 2011-12-12 at 10:25:41

newjeep19's gravatar image

newjeep19

If you have copy pasted my code it won't work and forget to mention one point in the previous comment

I have bolded the point, which I forget to mention in previous point as to decide what type of Input HTML Control it have to be from the list in the link
http://www.w3schools.com/html/html_forms.asp
http://www.javascript-coder.com/html-form/html-form-tutorial-p2.phtml
http://htmlhelp.com/reference/html40/forms/input.html

<input name="inPricePerUnit" value="<%# Eval("priceperunit", "{0:#,##0.#0}") %>" class="editFieldText75" onfocus="inPricePerUnit_OnData(<%= RowId() %>, this);" onkeypress="return noReturn(event);" runat="server" type=""/>
link

answered 2011-12-12 at 10:36:50

santhimurthyd's gravatar image

santhimurthyd

I did set the type to type="text" and I got the error
link

answered 2011-12-12 at 10:44:08

newjeep19's gravatar image

newjeep19

It's working fine for me, could you please check with adding the "id" attribute

if possible share the HTML syntax to check from my side
link

answered 2011-12-12 at 10:45:55

santhimurthyd's gravatar image

santhimurthyd

yes it's text........However, I need to only reset certian html tag's, checkboxes, etc.......So, in the code behind (the C# code that I submitted earlier) defines which types I want to reset.
i.e. txt1.Text = string.Empty;
however the above is looking for an asp,net tag and not an html type......i need to know how to have the C# code find the htlml type.....like it finds the asp.net tag.....
Thank you
link

answered 2011-12-12 at 11:07:12

newjeep19's gravatar image

newjeep19

In the previous comment  ID:37274397

Add the below using statement to get the HTML control
using System.Web.UI.HtmlControls


switch (c.GetType().ToString())
                    {
                        case "System.Web.UI.WebControls.TextBox": ((TextBox)c).Text = "";
                            break;
                        case "System.Web.UI.WebControls.CheckBox":
                            ((CheckBox)c).Checked = false;
                            break;

                          case "System.Web.UI.HtmlControls.HtmlInputText": ((HtmlInputText)c).Text = "";
                            break;
                       


                    }

bolded the syntax
link

answered 2011-12-12 at 11:20:21

santhimurthyd's gravatar image

santhimurthyd

c.).Text ="";  does not contain a definition for "Text"
Yes I do have :
System.Web.UI.HtmlControls.HtmlInput; declared
link

answered 2011-12-12 at 11:25:09

newjeep19's gravatar image

newjeep19

To get the context

Use "Value"

case "System.Web.UI.HtmlControls.HtmlInputText": ((HtmlInputText)c).Value= "";
                            break;


link

answered 2011-12-12 at 11:36:38

santhimurthyd's gravatar image

santhimurthyd

OK thank you.....but how do I specify which values that I want reset?
In this code example i am able to do that with this snipit of code:
  if (c.Controls.Count > 0)
                {
                    // ResetFormControlValues(c);
                    //txt1.Text = string.Empty;
                    //txt3.Text = string.Empty;
                    //txt4.Text = string.Empty;
                   // TextBox inListPrice = (TextBox(string.Empty));
                    TextBox priceText = (TextBox)parent.Item.Cells[3].Controls[0];

                   
                }
link

answered 2011-12-12 at 11:54:03

newjeep19's gravatar image

newjeep19

I think you can simplify your logic a bit as well:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
foreach (Control c in parent.Controls)
{
    ...
    else
    {
        System.Web.UI.HtmlControls.HtmlInputControl hic = c as System.Web.UI.HtmlControls.HtmlInputControl;

        if (hic != null)
        {
            hic.Value = string.Empty;
        }        
    }
}
link

answered 2011-12-12 at 12:00:21

kaufmed's gravatar image

kaufmed

HTML Input control, will go with "Value"

((HtmlInputText)c).Value= string.Empty;
link

answered 2011-12-12 at 12:02:14

santhimurthyd's gravatar image

santhimurthyd

Thank you but this does not clearify which ones to reset?
link

answered 2011-12-12 at 12:02:39

newjeep19's gravatar image

newjeep19

Could you please give more informationas which information you want to reset for the control

In your comment ID:37274456, the C# code have for loop to identify the type of controls and get value, that we haev did using the case with HTMLInputTextbox.

So, could you give more about the requirment
link

answered 2011-12-12 at 12:32:56

santhimurthyd's gravatar image

santhimurthyd

Here I state which asp.net textboxes that i want to reset:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
private void ResetFormControlValues(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.Controls.Count > 0)
                {
                    // ResetFormControlValues(c);
                    txt1.Text = string.Empty;
                    txt3.Text = string.Empty;
                    txt4.Text = string.Empty;
                    
                }
   
However, this is for asp.net and not a html type......the issue is on the ASP.NET aspx page the fields are html types and not asp.net ..... (I inherated this app)....so, instead of reenveting the wheel i want to have C# code that will reset only cerian html types...... like in the example above but not all of them.
Thank you
link

answered 2011-12-12 at 12:38:54

newjeep19's gravatar image

newjeep19

I feel that the example and the conversion to the aspx page and using statement hekpes you to get access to the HTML controls in C# code and now you want to remove any vlaue in the HTMLText box to empty based on condition.

Please correct me, if i'm wrong on understanding

private void ResetFormControlValues(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.Controls.Count > 0)
                {
                    // ResetFormControlValues(c);
                    txt1.Text = string.Empty;
                    txt3.Text = string.Empty;
                    txt4.Text = string.Empty;

//You can work on logic as what condition the value needs to be empty

                    ((HtmlInputText)c).Value = string.empty;
                                      or
                       HTMLInputText inputText = ((HtmlInputText)c);
                       inputText .Value = string.empty
                   
                }

if possible can you give more info "reset only cerian html types", what are the type of HTML control you want to reset andis their any specific condition the value to be reset.

I have place the code snippet for "HTMLInputTextBox" and you can type cast the control to the specific HTML type as did for TextBox

            System.Web.UI.HtmlControls.HtmlInputCheckBox
            System.Web.UI.HtmlControls.HtmlInputControl
            System.Web.UI.HtmlControls.HtmlInputRadioButton
            System.Web.UI.HtmlControls.HtmlInputText
            System.Web.UI.HtmlControls.HtmlLink
            System.Web.UI.HtmlControls.HtmlSelect

Please provide more info, if my understanding at your requirment is wrong
 
link

answered 2011-12-12 at 13:05:05

santhimurthyd's gravatar image

santhimurthyd

OK thank you for your reply,,,,you are correct ........I have a frid that contians html types
Ex:  (html type = name)
code:
 <asp:TemplateField HeaderText="Line #" ItemStyle-CssClass="gvCust cellSize40" SortExpression="lineitemnumber">
                                    <ItemTemplate>
                                        <input name="inLineItemNumber" value="<%# Eval("lineitemnumber") %>" class="editFieldText35" onfocus="inLineItemNumber_OnData(<%= RowId() %>, this);" onkeypress="return noReturn(event);" />
                                    </ItemTemplate>
                                </asp:TemplateField>
Another html type:
code:
  <asp:TemplateField HeaderText="Maint." ItemStyle-CssClass="gvGtri cellSize40" SortExpression="gtri_gsa">
                                    <ItemTemplate>
                                        <asp:CheckBox ID="inMaintenance" Checked='<%# Eval("gtri_maintenance") %>' runat="server" />
                                        <input type="hidden" name="hiMaintenance" value="<%# Eval("gtri_maintenance") %>" />
                                    </ItemTemplate>
                                </asp:TemplateField>
So, I need to reset ot clear the data in those fields.
This code here:
private void ResetFormControlValues(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.Controls.Count > 0)
                {
                    // ResetFormControlValues(c);
                    txt1.Text = string.Empty;
                    txt3.Text = string.Empty;
                    txt4.Text = string.Empty;

                   
                }
                else
                {
                    switch (c.GetType().ToString())
                    {
                        case "System.Web.UI.WebControls.TextBox": ((TextBox)c).Text = "";
                            break;
                        case "System.Web.UI.WebControls.CheckBox":
                            ((CheckBox)c).Checked = false;
                            break;
                    }
                }
            }
        }

only resets / clears data for asp:net text boxes but as you can see from the two examples I am not resetting data for asp.net boxes  I am resetting data for html types....
Thank you
link

answered 2011-12-12 at 20:46:33

newjeep19's gravatar image

newjeep19

Got it

In you first HTML synax, you may need to include two more attributes as follows

 <asp:TemplateField HeaderText="Line #" ItemStyle-CssClass="gvCust cellSize40" SortExpression="lineitemnumber">
       <ItemTemplate>
       <input name="inLineItemNumber" value="<%# Eval("lineitemnumber") %>" class="editFieldText35" onfocus="inLineItemNumber_OnData(<%= RowId() %>, this);" onkeypress="return noReturn(event);" runat="server" type="text" />
       </ItemTemplate>
</asp:TemplateField>

Where as the Second HTML syntax needs only the attribute runat="server"

<input type="hidden" name="hiMaintenance" value="<%# Eval("gtri_maintenance") %>"  runat="server" />

we need to add the attribute runat="server" to access the control on codebehind class of the page.

Then in the code behind, you need to do the below changes

i) Adding Using statement to the reference

using System.Web.UI.HtmlControls;

then in the method to reset the controls do the below changes

private void ResetFormControlValues(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.Controls.Count > 0)
                {
                    // ResetFormControlValues(c);
                    txt1.Text = string.Empty;
                    txt3.Text = string.Empty;
                    txt4.Text = string.Empty;

                   
                }
                else
                {
                    switch (c.GetType().ToString())
                    {
                        case "System.Web.UI.WebControls.TextBox":
                                  ((TextBox)c).Text = "";
                                  break;
                        case "System.Web.UI.WebControls.CheckBox":
                                 ((CheckBox)c).Checked = false;
                                 break;
                          case "System.Web.UI.HtmlControls.HtmlInputText":
                                   ((HtmlInputText)c).Value= "";
                                   break;
                           case "System.Web.UI.HtmlControls.HtmlInputHidden":
                                   ((HtmlInputHidden)c).Value= "";
                                   break;
                    }
                }
            }
        }


Please check and let me know as whether you able to meet your requirment
link

answered 2011-12-13 at 06:32:32

santhimurthyd's gravatar image

santhimurthyd

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×102
×156
×132

Asked: 12/12/2011 01:57

Seen: 325 times

Last updated: 12/16/2011 03:06