Solved

Onclick Command to Propagate Textbox

Posted on 2008-06-19
9
758 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
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!

 
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
 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET - Why is accordion not working? 3 47
disadvantage of code behind appoarch in asp.net 3 28
Making Table Thru ASP Response.write 5 19
asp.net repeater 2 7
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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