Solved

Onclick Command to Propagate Textbox

Posted on 2008-06-19
9
749 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
ID: 21824523
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
ID: 21824619
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
ID: 21824679
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
ID: 21824793
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:misnstt
ID: 21824859
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
ID: 21824881
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
ID: 21824933
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
ID: 21824965
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
ID: 31468882
Cheers
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

15 Experts available now in Live!

Get 1:1 Help Now