Solved

panel click in gridview code behind

Posted on 2008-10-22
27
998 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
ID: 22779054
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
ID: 22779065
onclick="trythis();"  instead put:  onclick="trythis"
0
 
LVL 21

Expert Comment

by:silemone
ID: 22779083
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 21

Expert Comment

by:silemone
ID: 22779105
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
ID: 22779143
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
ID: 22779222
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
ID: 22779260
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
ID: 22779430
@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
ID: 22779797
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
ID: 22779921
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
ID: 22779943
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
ID: 22780034
@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
ID: 22780196
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
 

Author Comment

by:brettawv
ID: 22780318
I keep getting an error from the .aspx page saying Name'hidbutton' is not defined.
0
 

Author Comment

by:brettawv
ID: 22780391
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
ID: 22780425
let me look over...
0
 
LVL 21

Expert Comment

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

Expert Comment

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

Expert Comment

by:samtran0331
ID: 22781136
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
ID: 22781176
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
ID: 22785703
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
ID: 22786239
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
ID: 22786257
0
 

Author Comment

by:brettawv
ID: 22786899
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
ID: 22786997
yes, use accept for sam and assist for me
0
 
LVL 21

Expert Comment

by:silemone
ID: 22787095
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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