Solved

Onclick Command to Propagate Textbox

Posted on 2008-06-19
9
743 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:misnstt
  • 3
  • 3
  • 3
9 Comments
 
LVL 22

Expert Comment

by:prairiedog
Comment Utility
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
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
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
 
LVL 22

Expert Comment

by:prairiedog
Comment Utility
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
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:misnstt
Comment Utility
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
 
LVL 22

Accepted Solution

by:
prairiedog earned 250 total points
Comment Utility
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
 
LVL 37

Assisted Solution

by:samtran0331
samtran0331 earned 250 total points
Comment Utility
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
 

Author Comment

by:misnstt
Comment Utility
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
 

Author Closing Comment

by:misnstt
Comment Utility
Cheers
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now