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

misnsttAsked:
Who is Participating?
 
prairiedogConnect With a Mentor Commented:
Agreed samtran0331. It is always good to know there is an alternative.

Oops, forgot the author is using C#. Here is the C# version of my code:
protected void LinkButton1_Click(Object sender, System.EventArgs e )
{
        LinkButton  lbtn = (LinkButton)sender;
        DataListItem dlr = (DataListItem)lbtn.NamingContainer;
        Label lbl = (Label)dlr.FindControl("Day1Label");
        this.SelectedDate1.Text = lbl.Text;
}
0
 
prairiedogCommented:
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

0
 
samtran0331Commented:
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

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
prairiedogCommented:
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
0
 
samtran0331Commented:
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>
0
 
misnsttAuthor Commented:
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
0
 
samtran0331Connect With a Mentor Commented:
and the c# version of mine:
<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "SelectFirst":
                this.TextBox1.Text = ((Label)e.Item.FindControl("FirstNameLabel")).Text;
                break;
            case "SelectLast":
                this.TextBox1.Text = ((Label)e.Item.FindControl("LastNameLabel")).Text;
                break;
        } 
    }
</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

0
 
misnsttAuthor Commented:
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.  
0
 
misnsttAuthor Commented:
Cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.