Passing Form fields which have been generated by User Control

随声附和 提交于 2019-12-11 12:44:16

问题


I'm having real troubles passing form data from a posting page made up of User Controls.

I posted this question before, here, Getting form field names on Submit when form is user control but the answers have not solved my problem, or I have mis-understood them.

Let me try explaining what I am doing more clearly.

I have a page which displays a form to the user. The form is made of several sections, but for simplicity we'll say there are two. Each of these sections is a User Control, thus the actual markup for newSale.aspx is just:

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" CodeFile="newSale.aspx.vb" Inherits="newSale_Default"%>
 <%@ Register TagPrefix="controls" TagName="customerForm" Src="~/Controls/customerForm.ascx" %>
 <%@ Register TagPrefix="controls" TagName="itemList" Src="~/Controls/itemList.ascx" %>

 <asp:content id="content1" ContentPlaceHolderID="mainContent" runat="server">
 <div class="content">  
 <form id="newSale" method="post" action="saveSale.aspx">
 <h1>--- New Sale ---</h1>  
    <div class="section" id="customerSection">        
        <controls:customerForm ID='customerForm1' showBankDetails="false" runat='server' />
    </div>
    <div class="section" id="saleSection">       
        <controls:itemList ID='itemList1' showShipping="false" showDeposit="false" runat='server' />        
        <div class="row submitRow">
            <div id="submitSale">Submit</div>
        </div>
    </div>
    </form>    
</div>
</asp:content>

So you see my two main Controls tags with the IDs "customerForm1" and "itemList1" respectively.

The submit button is deliberately a clickable div and submission is done by jQuery, thus:

$('#submitSale').live('click', function () { $('#newSale').submit(); }); 

As the User Controls are rendered into the browser, the IDs and Names of the elements are messed about (I understand the reason why) by ASP.NET, so taking one field as an example:

A field named "name" contained within the customerForm control becomes m$mainContent$customerForm1$name

  • 'm' - being the ID of my MasterPage
  • 'mainContent' - being the ID of the PlaceHolder
  • 'customerForm1' - being the ID of the User Control and
  • 'name' being the element's actual name.

I wish to submit this form to a separate file (saveSale.aspx) as per the action attribute of my Form tag declared above.

Within the Code-Behind of saveSale.aspx I need to save the data passed from the form into a dataBase and return it in the aspx file as an HTML page.

How do I reliably get the value of the submitted form fields in saveSale.aspx(.vb)?

<%= request.form("m$mainContent$customerForm1$name") %>

Works, but naturally is a pain to use, especially when I want to replicate this to other purposes.

m.mainContent.customerForm1.name.selectedValue()

tells me that 'm' is not declared

and from the other replies to my previous question I have tried registering the controls at the top of the (saveSale.aspx) page, and explicitly posting the Control into the page again with

<controls:customerForm ID='customerForm1' showBankDetails="false" runat='server' />

Which doesn't work, but even if it did, it makes no sense anyway as I don't want to use the Control anymore, I just want to get the data from it.

I do apologise if I'm being dumb here, but I have tried so many different variations of code and Googled ideas, but I can't seem to make this work in any scalable fashion beyond the Request.Form example above.

I am using .NET 2.0 so can't use the Static Client feature that I've seen mentioned for .NET 4.0

Also, please, I am using VB.NET, so if you can help, please help with a VB example.

Many thanks.

来源:https://stackoverflow.com/questions/8390146/passing-form-fields-which-have-been-generated-by-user-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!