Solved

panel click in gridview code behind

Posted on 2008-10-22
27
971 Views
Last Modified: 2012-05-05
I have a gridiew that is using a panel in a cell.  I'm using this primarily to get a click event to fire and run some code in my code behind file (<asp:Panel ID="pan1" onclick="trythis();" runat="server" >).  When I run the app and click on the cell that has the panel I get an error from internet explorer that says (Error: 'trythis' is undefined).
However I have my codebehind with this sub.
(Protected Sub trythis()
        Response.Write("Found You")
    End Sub)
Is what I'm doing possible?  If so, how?
Thanks
0
Comment
Question by:brettawv
  • 14
  • 7
  • 5
27 Comments
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
In your code:
<asp:Panel ID="pan1" onclick="trythis();" runat="server" >

the onclick is a client (javascript) event, and is in no way related to your server code...so you're getting the error because the browser is looking for a javascript function called "trythis"

while it is possible to trigger a server event from a javascript event, it gets messy really quick and involves calling the ASP.Net javascript "__doPostBack" event...

What is it you're tring to do with the panel in a cell? what is in the panel?
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
onclick="trythis();"  instead put:  onclick="trythis"
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
I'm assuming that you are using c# for the trythis() code as you said it's in the code behind.

By the way, if you're only using the Grid because you want postback that are partial, you should use an
UpdatePanel with the ChildrenAsTriggers=true   <--that updates whenever a child of the panel event is called.
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
in this:  onclick="trythis();"  instead put:  onclick="trythis"

you're calling onclick like you would in javascript...however when doing onclick events in control tags, there are delegates that will look for this event....another problem is:  

(Protected Sub trythis()
        Response.Write("Found You")
    End Sub)
since you're calling this from an event...it should be...

Protected Sub trythis(Object sender, EventArgs e)
        Response.Write("Found You")
    End Sub
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
if your code is in vb...then

        Private Sub trythis(ByVal sender As Object, ByVal e As System.EventArgs) handles pan1.Click

 and you don't even have to add onclick = " "  it will know already from the handles portion

0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
Noting that pan1 doesn't have an onclick event, now you have to:

pnl1.Attributes.Add("onclick",  "trythis()")   <---in page load




0
 

Author Comment

by:brettawv
Comment Utility
You've given me several options to try.  Thanks and I'll let you know how it goes.
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
@silemone:
>>however when doing onclick events in control tags, there are delegates that will look for this event
True

>>Noting that pan1 doesn't have an onclick event, now you have to:
>>pnl1.Attributes.Add("onclick",  "trythis()")

when you add the onclick attribute, you're not delegating anything, you merely add the javascript "onclick" to the rendered html of the panel...which really puts the asker right back where he started.
0
 

Author Comment

by:brettawv
Comment Utility
Here's my dilemma.  I've built an intranet site and I'm trying to get a Fileupload control in my gridview to fill a textbox in the same cell with the filename.  I want this to happen when I click the FileUpload button.  I was trying the panel because I thought this might help me with the click event since the Fileupload button didn't have a click event.  I'm probably making this much more complicated than it has to be.  Is there a simple solution?
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
i agree sam...i was going to add this but i had typed too much and was waiting for a comment...


inside the

In .aspx:

<script type="text/javascript">

function trythis()
{
          var elem = document.getElementById("<%= hidButton.ClientID %>")
          elem.click()
}

</script


<asp:UpdatePanel id="uPanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
       <ContentTemplate>
                <asp:Panel ID="pan1" onclick="trythis();" runat="server" >.....</asp:Panel>
                <asp:LinkButton id="hidButton" runat="server">
       </ContentTemplate>
</asp:UpdatePanel

in code behind:

pnl1.Attributes.Add("onclick",  "trythis()")   <-- in page load


   'button event
Private Sub trythis(ByVal sender As Object, ByVal e As System.EventArgs) handles pan1.Click
           Response.Write("Found You")
End Sub
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
UpdatePanel will help events fire because of --->ChildrenAsTriggers="true"   so everytime hidButton is fired, upPanel will will have a partial refresh...
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
@brettawv:
>>I'm trying to get a Fileupload control in my gridview to fill a textbox in the same cell with the filename.

...so in addition to the textbox that gets rendered with the fileupload control, you want another textbox with the same text (filename)?

0
 

Author Comment

by:brettawv
Comment Utility
Yes that is true.  I'm using he control to get the filename that will be stored in a sql database and bound to a hyperlink field so the user can click on the hyperlink to view there file.  Usually a pdf.
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:brettawv
Comment Utility
I keep getting an error from the .aspx page saying Name'hidbutton' is not defined.
0
 

Author Comment

by:brettawv
Comment Utility
OK - Now I'm getting Object reference not set to an instance of an object for the hidbutton.clientid.  How do I use the new keyword here.
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
let me look over...
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
you're not making hidbutton invisible=true are you?
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
also you are using the right casing?  i.e. hidVal  not equal to hidval
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
What silemone posted:
function trythis()
{
var elem = document.getElementById("<%= hidButton.ClientID %>")
elem.click()
}


within the context of a grid, there isn't going to be a "hidButton" available (assuming each row has a "hidButton")  to the javascript because .Net will name each row's fileupload differently... and there really isn't a "hidButton" control available to the code because it is in a grid....
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 250 total points
Comment Utility
The only reason I could think of that you might want to put the value of a fileupload into another textbox would be to maybe have a running list in the textbox of the files you selected (?)...

so if your goal is simply to copy the selected filename from the fileupload into another textbox in the same row of the grid...below is a test page with a screenshot of the results...
it's bound to Northwind simply so I could have some grid data...

everytime you click "browse" for a file upload...it adds the value of the file upload to the textbox...
<%@ 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 GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim t As TextBox = CType(e.Row.FindControl("TextBox1"), TextBox)

            Dim f As FileUpload = CType(e.Row.FindControl("FileUpload1"), FileUpload)

            f.Attributes.Add("onchange", "javascript:ConcatText('" & t.ClientID & "','" & f.ClientID & "');")

        End If

    End Sub

</script>
 

<script language="javascript" type="text/javascript">

    function ConcatText(tb1,tb2) //tb1 = textbox, tb2 = fileupload

    {

      document.getElementById(tb1).value = document.getElementById(tb1).value + document.getElementById(tb2).value + '\n\n' 

    }

</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:NorthwindConnectionString %>" SelectCommand="SELECT [EmployeeID], [LastName] FROM [Employees]"></asp:SqlDataSource>

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">

                <Columns>

                    <asp:TemplateField HeaderText="Upload List" ItemStyle-VerticalAlign="top">

                        <ItemTemplate>

                            <asp:TextBox ID="TextBox1" runat="server" Height="149px" TextMode="MultiLine" Width="330px"></asp:TextBox>

                        </ItemTemplate>

                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Upload" ItemStyle-VerticalAlign="top">

                        <ItemTemplate>

                            <asp:FileUpload ID="FileUpload1" runat="server" />

                        </ItemTemplate>

                    </asp:TemplateField>

                </Columns>

            </asp:GridView>

        </div>

    </form>

</body>

</html>

Open in new window

ScreenShot.jpg
0
 

Author Comment

by:brettawv
Comment Utility
I'm so close...  
At this line:
f.Attributes.Add("onchange", "javascript:CopyText('" & t.ClientID & "','" & f.ClientID & "');")
It tells me (object reference not set to an instance of an object.)

I know it's looking for a new reference but not sure how to do that with the line above.
0
 
LVL 21

Assisted Solution

by:silemone
silemone earned 250 total points
Comment Utility
if these controls are inside of a grid, brett, then the reference you have to get has to come from inside the grid...

you would use something like:  
TextBox b;
b = DataGrid1.Items[DataGrid1.SelectedIndex].FindControl("TextBox1") as TextBox;



0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
0
 

Author Comment

by:brettawv
Comment Utility
I had to use a try catch in the rowdatabound.  I have code in the rowupdating sub which causes the rowdatabound to fire.  Once I did that everything worked perfect.  You guys are great!  Do you know how I'm able to split the points between the two of you because you both helped.  If I'm unable to do that I'm going to give the points to samtran0331 because it was his could that ultimately got me to the solution.

Thanks again guys.  
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
yes, use accept for sam and assist for me
0
 
LVL 21

Expert Comment

by:silemone
Comment Utility
Accept multiple solutions
    If several Experts collaborated to provide a solution, use this option and split the points among them.  

Anyway...good luck...
0

Featured Post

Highfive Gives IT Their Time Back

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

13 Experts available now in Live!

Get 1:1 Help Now