panel click in gridview code behind

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
brettawvAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

samtran0331Commented:
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
silemoneCommented:
onclick="trythis();"  instead put:  onclick="trythis"
0
silemoneCommented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

silemoneCommented:
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
silemoneCommented:
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
silemoneCommented:
Noting that pan1 doesn't have an onclick event, now you have to:

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




0
brettawvAuthor Commented:
You've given me several options to try.  Thanks and I'll let you know how it goes.
0
samtran0331Commented:
@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
brettawvAuthor Commented:
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
silemoneCommented:
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
silemoneCommented:
UpdatePanel will help events fire because of --->ChildrenAsTriggers="true"   so everytime hidButton is fired, upPanel will will have a partial refresh...
0
samtran0331Commented:
@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
brettawvAuthor Commented:
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
brettawvAuthor Commented:
I keep getting an error from the .aspx page saying Name'hidbutton' is not defined.
0
brettawvAuthor Commented:
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
silemoneCommented:
let me look over...
0
silemoneCommented:
you're not making hidbutton invisible=true are you?
0
silemoneCommented:
also you are using the right casing?  i.e. hidVal  not equal to hidval
0
samtran0331Commented:
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
samtran0331Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brettawvAuthor Commented:
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
silemoneCommented:
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
silemoneCommented:
0
brettawvAuthor Commented:
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
silemoneCommented:
yes, use accept for sam and assist for me
0
silemoneCommented:
Accept multiple solutions
    If several Experts collaborated to provide a solution, use this option and split the points among them.  

Anyway...good luck...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

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.