Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1025
  • Last Modified:

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
0
brettawv
Asked:
brettawv
  • 14
  • 7
  • 5
2 Solutions
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 14
  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now