Link to home
Start Free TrialLog in
Avatar of misnstt
misnstt

asked on

Onclick Command to Propagate Textbox

I have a DataList which contains a textfield called Day1 and gets its information from a SQL Datasource.  On the same datalist is a LinkButton.  
I have a Textbox  called SelectedDate1 located outside of the Datalist that i want populated with the  information from the Day1 textfield then the  Link Button is clicked.

I need help with coding the linkbutton to do as needed when pressed.  I am a beginner at this so a detailed explanation is very helpful.  Thanks
THIS IS THE DATALIST:
<asp:DataList ID="DataList8" runat="server" DataKeyField="PayPeriodId" 
                            DataSourceID="SqlDataSource13">
                            <ItemTemplate>
                                Day1:
                                <asp:Label ID="Day1Label" runat="server" Text='<%# Eval("Day1") %>' />
                                <br />
                                <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
                                <br />
                                <br />
                                PayPeriodNo:
                                <asp:Label ID="PayPeriodNoLabel" runat="server" 
                                    Text='<%# Eval("PayPeriodNo") %>' />
                                <br />
                                Day8:
                                <asp:Label ID="Day8Label" runat="server" Text='<%# Eval("Day8") %>' />
                                <br />
                                Day15:
                                <asp:Label ID="Day15Label" runat="server" Text='<%# Eval("Day15") %>' />
                                <br />
                                Day22:
                                <asp:Label ID="Day22Label" runat="server" Text='<%# Eval("Day22") %>' />
                                <br />
                                <br />
                            </ItemTemplate>
                        </asp:DataList>
 
THIS IS THE TEXTBOX LOCATED OUTSIDE OF THE DATALIST:
            <asp:TextBox ID="SelectedDate1" runat="server"></asp:TextBox>

Open in new window

Avatar of prairiedog
prairiedog
Flag of United States of America image

This should do:
Protected Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim lbtn As LinkButton = CType(sender, LinkButton)
        Dim dlr As DataListItem = CType(lbtn.NamingContainer, DataListItem)
        Dim lbl As Label = CType(dlr.FindControl("Day1Label"), Label)
        Me.SelectedDate1.Text = lbl.Text
End Sub

Open in new window

alternately, you can use the datalist's itemcommand to do the same thing; prairiedog's method will work fine, but for controls that do have an itemcommand, I personally like to make use of it.
<%@ Page Language="VB" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
        Select Case e.CommandName
            Case "SelectFirst"
                Me.TextBox1.Text = CType(e.Item.FindControl("FirstNameLabel"), Label).Text
            Case "SelectLast"
                Me.TextBox1.Text = CType(e.Item.FindControl("LastNameLabel"), Label).Text
        End Select
    End Sub
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>"
                SelectCommand="SELECT [EmployeeID], [LastName], [FirstName] FROM [Employees]"></asp:SqlDataSource>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><hr />
            <asp:DataList ID="DataList1" runat="server" DataKeyField="EmployeeID" DataSourceID="SqlDataSource1"
                OnItemCommand="DataList1_ItemCommand">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("EmployeeID") %>'
                        CommandName="SelectFirst">Set First Name To Textbox</asp:LinkButton>&nbsp;&nbsp;&nbsp;
                    <asp:LinkButton ID="LinkButton2" runat="server" CommandArgument='<%# Eval("EmployeeID") %>'
                        CommandName="SelectLast">Set Last Name To Textbox</asp:LinkButton><br />
                    LastName:
                    <asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>'></asp:Label><br />
                    FirstName:
                    <asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label><br />
                    <hr />
                </ItemTemplate>
            </asp:DataList>
        </div>
    </form>
</body>
</html>

Open in new window

samtran0331,
I agree. ItemCommand gives you a lot of contorl.
The answer I gave is based on the author's current design of the page.

prairiedog
Understood prairiedog,
As misnstt posted he/she is a beginner, it's good they know there is more than one way to do this.

Also, misnstt, to see prairiedog's code work, be sure to wire up the linkbutton to use his sub...
this:
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>

would be:
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
Avatar of misnstt
misnstt

ASKER

Thanks for the fast and comprehensive responses.  I should have stated that im using C# and I believe your responses are for VB.  Should I do it differently for C#
Thanks
ASKER CERTIFIED SOLUTION
Avatar of prairiedog
prairiedog
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of misnstt

ASKER

Thank you Samtran0331 and prairiedog for your assistance.  I used prairiedog's example and it worked perfectly.  I am also working with samtran0331 example for something else I want to do in the future.  
Avatar of misnstt

ASKER

Cheers