Solved

Code-Behind Method from dynamically created HTML

Posted on 2011-02-13
3
571 Views
Last Modified: 2012-05-11
Hi All

I would like to know if it is possible to dynamically create asp:LinkButton that post back to code behind with a value in the CommandArgument field.
Meaning I have a list of borrowers and would like to give the user the ability to edit the borrower, but can not use a gridview.

I would like to edit the borrower on the same place I use to add a new borrower.
The code I attached shows what I did, but was not sure as to where to go next or even if I'm doing anything wrong, because my code behind method (also attached) does not have any value in the CommandArgument field.

Any ideas?

S
<table>
                                    <tr>
                                        <th>
                                            Application For Cover - Borrower(s) Information
                                        </th>
                                    </tr>
                                    <tr>
                                        <td>
                                            <% if (HFGABLL.CDatabaseTasks.GetTempBorrowers(Convert.ToInt32(Session["ID"])).Count() != 0)
                                               { %>
                                            <table width="100%" class="Contents1" border="0" cellpadding="0" cellspacing="0">
                                                <tr>
                                                    <th align="left">
                                                        Full Name
                                                    </th>
                                                    <th align="left">
                                                        ID Ref
                                                    </th>
                                                    <th align="left">
                                                        Gross Monthly Income
                                                    </th>
                                                    <% if (Policy.UseNetIncome)
                                                       { %>
                                                    <th align="left">
                                                        Nett Monthly Income
                                                    </th>
                                                    <%} %>
                                                    <th align="left">
                                                        Primary Borrower
                                                    </th>
                                                    <th align="left">
                                                        Edit
                                                    </th>
                                                    <th align="left">
                                                        Remove
                                                    </th>
                                                </tr>
                                                <% bool bAltRow = false;
                                                   foreach (var pBor in HFGABLL.CDatabaseTasks.GetTempBorrowers(Convert.ToInt32(Session["ID"])))
                                                   { %>
                                                <tr <% if (bAltRow) Response.Write("class=\"AltRow\""); %>>
                                                    <td align="left">
                                                        <%=pBor.FirstName + " " + pBor.LastName %>
                                                    </td>
                                                    <td align="left">
                                                        <%=pBor.IDReference %>
                                                    </td>
                                                    <td align="left">
                                                        <%=DisplayFmt.Money(pBor.MonthlyIncome) %>
                                                    </td>
                                                    <% if (Policy.UseNetIncome)
                                                       { %>
                                                    <td align="left">
                                                        <%=DisplayFmt.Money(pBor.NettIncome)%>
                                                    </td>
                                                    <%} %>
                                                    <td align="left">
                                                        <%=DisplayFmt.Boolean(pBor.PrimaryBorrower) %>
                                                    </td>
                                                    <td align="left">
                                                        <asp:LinkButton ID="LinkButton2" runat="server" CommandArgument='<%# Eval("ID") %>'
                                                            Text="Edit" OnCommand="DoEditBorrower" />
                                                    </td>
                                                    <td align="left">
                                                        <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#Eval("pBor.ID") %>'
                                                            Text="Edit" OnCommand="DoEditBorrower" />
                                                    </td>
                                                </tr>
                                                <%bAltRow = !bAltRow;
                                                   } %>
                                            </table>
                                            <%} %>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:PlaceHolder ID="phBorrowers" runat="server" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <table>
                                                <tr>
                                                    <td align="left">
                                                        Gross Monthly Income
                                                    </td>
                                                    <td align="left">
                                                        <asp:TextBox ID="txtMonthlyIncome" runat="server" Width="140" />
                                                        <asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" TargetControlID="txtMonthlyIncome"
                                                            FilterType="Numbers, Custom" ValidChars=",." />
                                                        <%=strCurrencyCode %>
                                                    </td>
                                                </tr>
                                                <% if (Policy.UseNetIncome)
                                                   { %>
                                                <tr>
                                                    <td align="left">
                                                        Nett Monthly Income
                                                    </td>
                                                    <td align="left">
                                                        <asp:TextBox runat="server" ID="txtMonthlyIncomeNett" MaxLength="30" ValidationGroup="MKE" />
                                                        <%=strCurrencyCode %>
                                                        <asp:Label ID="lblErrorOntxtMonthlyIncomeNett" runat="server" CssClass="ErrorLabel"
                                                            Visible="false" Text="*" />
                                                        <asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender10" runat="server" TargetControlID="txtMonthlyIncomeNett"
                                                            FilterType="Numbers, Custom" ValidChars=",." />
                                                    </td>
                                                </tr>
                                                <%} %>
                                                <tr>
                                                    <td align="left">
                                                        Title
                                                    </td>
                                                    <td align="left">
                                                        <asp:DropDownList ID="ddlTitle" runat="server" Width="140" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="left">
                                                        Initials<asp:RequiredFieldValidator ID="RequiredFieldValidator13" runat="server"
                                                            ErrorMessage="*" ControlToValidate="txtInitials" ValidationGroup="AddBorrower" />
                                                    </td>
                                                    <td align="left">
                                                        <asp:TextBox ID="txtInitials" runat="server" Width="140" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="left">
                                                        First Name<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server"
                                                            ErrorMessage="*" ControlToValidate="txtFirstName" ValidationGroup="AddBorrower" />
                                                    </td>
                                                    <td align="left">
                                                        <asp:TextBox ID="txtFirstName" runat="server" Width="140" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="left">
                                                        Last Name<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server"
                                                            ErrorMessage="*" ControlToValidate="txtLastName" ValidationGroup="AddBorrower" />
                                                    </td>
                                                    <td align="left">
                                                        <asp:TextBox ID="txtLastName" runat="server" Width="140" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="left">
                                                        ID Type
                                                    </td>
                                                    <td align="left">
                                                        <asp:DropDownList ID="ddlIDType" runat="server" Width="140" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="left">
                                                        ID Reference/No<asp:RequiredFieldValidator ID="RequiredFieldValidator18" runat="server"
                                                            ErrorMessage="*" ControlToValidate="txtIDRef" ValidationGroup="AddBorrower" />
                                                    </td>
                                                    <td align="left">
                                                        <asp:TextBox ID="txtIDRef" runat="server" Width="140" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="left">
                                                        Primary Borrower
                                                    </td>
                                                    <td align="left">
                                                        <asp:CheckBox ID="cbPrimBorrower" runat="server" Width="140" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="left">
                                                        Telephone<asp:RequiredFieldValidator ID="RequiredFieldValidator19" runat="server"
                                                            ErrorMessage="*" ControlToValidate="txtTelephone" ValidationGroup="AddBorrower" />
                                                    </td>
                                                    <td align="left">
                                                        <asp:TextBox ID="txtTelephone" runat="server" Width="140" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="left">
                                                        Cellphone<asp:RequiredFieldValidator ID="RequiredFieldValidator20" runat="server"
                                                            ErrorMessage="*" ControlToValidate="txtCellPhone" ValidationGroup="AddBorrower" />
                                                    </td>
                                                    <td align="left">
                                                        <asp:TextBox ID="txtCellPhone" runat="server" Width="140" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="left">
                                                        Address<asp:RequiredFieldValidator ID="RequiredFieldValidator21" runat="server" ErrorMessage="*"
                                                            ControlToValidate="txtAddressBorrower" ValidationGroup="AddBorrower" />
                                                    </td>
                                                    <td align="left">
                                                        <asp:TextBox ID="txtAddressBorrower" runat="server" TextMode="MultiLine" Width="140" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="left">
                                                        Suburb<asp:RequiredFieldValidator ID="RequiredFieldValidator22" runat="server" ErrorMessage="*"
                                                            ControlToValidate="txtSuburbBorrower" ValidationGroup="AddBorrower" />
                                                    </td>
                                                    <td align="left">
                                                        <asp:TextBox ID="txtSuburbBorrower" runat="server" Width="140" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="left">
                                                        City/Town<asp:RequiredFieldValidator ID="RequiredFieldValidator23" runat="server"
                                                            ErrorMessage="*" ControlToValidate="txtCityBorrower" ValidationGroup="AddBorrower" />
                                                    </td>
                                                    <td align="left">
                                                        <asp:TextBox ID="txtCityBorrower" runat="server" Width="140" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td align="left">
                                                        Area (Postal) code<asp:RequiredFieldValidator ID="RequiredFieldValidator24" runat="server"
                                                            ErrorMessage="*" ControlToValidate="txtCodeBorrower" ValidationGroup="AddBorrower" />
                                                    </td>
                                                    <td align="left">
                                                        <asp:TextBox ID="txtCodeBorrower" runat="server" Width="140" />
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblResultOnBorrowerStep" runat="server" CssClass="ErrorLabel" Visible="false" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <table>
                                                <tr>
                                                    <td>
                                                        <asp:Label ID="lblBorrowerID" runat="server" Visible="false" />
                                                        <asp:Button runat="server" ID="btnClearBorrowers" Text="Clear Borrowers" OnClick="btnClearBorrowers_Click" />
                                                        <asp:Button runat="server" ID="btnAddBorrower" Text="Add Borrower" OnClick="btnAddBorrower_Click"
                                                            ValidationGroup="AddBorrower" />
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </table>

Open in new window

protected void DoEditBorrower(object sender, CommandEventArgs e)
    {
        throw new Exception(e.CommandArgument.ToString());
    }

Open in new window

0
Comment
Question by:SeanNij
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Expert Comment

by:jondow
ID: 34933531
Hi there,

The following works:

 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.LinkButton1.CommandArgument = "test"
    End Sub
    Sub DoEditBorrower(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim x = "You chose: " & e.CommandName & " Item " & e.CommandArgument
    End Sub

Open in new window


 
<asp:LinkButton id="LinkButton1" 
           Text="Edit"
           CommandName="Borrower" 
           CommandArgument=""
           OnCommand="DoEditBorrower" 
           runat="server"/>

Open in new window


If memory serves me correctly, an arguement set as you are trying to is not available on postback due to the page lifecycle, although if anyone would like to prove me wrong feel free.

Regards

Rick
0
 
LVL 1

Accepted Solution

by:
SeanNij earned 0 total points
ID: 34958070
Hi Rick

Not what I was looking for but came right with javascript,

Thanks.
0
 
LVL 1

Author Closing Comment

by:SeanNij
ID: 34990904
The user that commented did not provide a solution. I did eventually figure it out by myself.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question