简体   繁体   中英

Methods similar to Request.Form for setting values instead of reading them?

I'm experienced with using Request.Form["someName"] to pull the value held by a control, but am looking for a similar method that will allow me to set the value of a control.

The code would be a lot to copy paste, but here are a few reasons why I am needing this specific method.

  1. Rows in a table are being copied and inserted into the HTML via JavaScript - this disables my ability to use "FindControl" to find the controls (I've already tried this)

  2. I'm ultimately looking to do validation of these controls. Imagine the following (VERY simplified example).

     <table> <tr> <td> <asp:TextBox id="txt__1" runat="server" /> <asp:CustomValidator ID="cvalTxt__1" /> </td> </tr> </table> 

On a certain button click, the table row is copied and inserted below to create the following (via JavaScript, which is why I cannot explicitly set values)

    <table>
        <tr>
          <td>
             <asp:TextBox id="txt__1" runat="server" />
             <asp:CustomValidator ID="cvalTxt__1" />
          </td>
        </tr>
        <tr>
          <td>
             <asp:TextBox id="txt__2" runat="server" />
             <asp:CustomValidator ID="cvalTxt__2" />
          </td>
        </tr>
    </table>

I have a functional way to get the values of these controls in the codebehind (C#) using Request.Form and the container's UniqueID property, but need a way to set the custom validator based on the content of several other textboxes in this form.

Are there methods similar to Request.Form that I could use to set the value of a specific validator in this table to true from my C# code behind?

Thank you for any suggestions.

The way to access a control and its contents and properties is by accessing the object that represents the control. For example. instead of reading Form[MyControl.UniqueID] you should use this.MyControl.Text . In a similar fashion you can set the contents, eg using this.MyControl.Text = "Some value" .

To set a validator's property, use

this.MyValidator.SomeProperty = someValue;

If the validator was created dynamically, you should be able to access it using the Page.Validators collection.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM