How can I take more control in ASP.NET?

前端 未结 7 663
感情败类
感情败类 2020-12-22 16:25

I\'m trying to build a very, very simple \"micro-webapp\" which I suspect will be of interest to a few Stack Overflow\'rs if I ever get it done. I\'m hosting it on my C# in

7条回答
  •  别那么骄傲
    2020-12-22 17:04

    This solution will give you programmatic access to the controls in their entirety including all attributes on the controls. Also, only the text box values will appear in the URL upon submission so your GET request URL will be more "meaningful"

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JonSkeetForm.aspx.cs" Inherits="JonSkeetForm" %>
    
    
    
    
        Jon Skeet's Form Page
    
    
        
    Some text

    Then in your code-behind you can do everything you need on PageLoad

    public partial class JonSkeetForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            text1.Value = Request.QueryString[text1.ClientID];
            text2.Value = Request.QueryString[text2.ClientID];
        }
    }
    

    If you don't want a form that has runat="server", then you should use HTML controls. It's easier to work with for your purposes. Just use regular HTML tags and put runat="server" and give them an ID. Then you can access them programmatically and code without a ViewState.

    The only downside is that you won't have access to many of the "helpful" ASP.NET server controls like GridViews. I included a Repeater in my example because I'm assuming that you want to have the fields on the same page as the results and (to my knowledge) a Repeater is the only DataBound control that will run without a runat="server" attribute in the Form tag.

提交回复
热议问题