Solved

Asp.net - DataGrid - Item Command

Posted on 2004-10-30
8,008 Views
Last Modified: 2007-12-19
Hi, another asp.net Q.

Ok here is what I want to do...

I have a datagrid filled with items, 1 of the columns I have set a command for... which when
clicked displays the details of the products on the same page, below the grid...that's working just
fine... like this:
        If e.CommandName = "ShowDetails" Then

                Try
                   'This load the details
                    GetItemDetails(e.Item.Cells(0).Text)

                Catch ex As Exception
                    lblTitle.Text = "itm command " & ex.Message.ToString
                Finally
                    '
                End Try
            End If
   
What I want to do is I have added a check box, show details in new window.

so the code shoud look something like
        If e.CommandName = "ShowDetails" Then
            'Check if pop new window is checked.
            If cbPopDetails.Checked = True Then 'Show details in new widow

          response.redirect(whatever it takes for a new window)
          'This preferably will use a javascript pop up, so I can set size, position
          'and scroll, toolbars, etc.
            Else
           'Show details on the same page.

                Try
                    GetItemDetails(e.Item.Cells(0).Text)

                Catch ex As Exception
                    lblTitle.Text = "itm command " & ex.Message.ToString
                Finally
                    '
                End Try
            End If
        End If

The new window code for the javascript is:
<SCRIPT language="JavaScript">
<!--
function na_open_window(name, url, left, top, width, height, toolbar, menubar, statusbar, scrollbar, resizable)
{
  toolbar_str = toolbar ? 'yes' : 'no';
  menubar_str = menubar ? 'yes' : 'no';
  statusbar_str = statusbar ? 'yes' : 'no';
  scrollbar_str = scrollbar ? 'yes' : 'no';
  resizable_str = resizable ? 'yes' : 'no';
  window.open(url, name, 'left='+left+',top='+top+',width='+width+',height='+height+',toolbar='+toolbar_str+',menubar='+menubar_str+',status='+statusbar_str+',scrollbars='+scrollbar_str+',resizable='+resizable_str);
}

// -->
</SCRIPT>

The field in the grid that is clicked obvioussly is not a hyperlink field and I would prefer not to use
that is possible, but I want a user to be able to view the item either way.

Thanks in advance!
0
Question by:flosoft
    26 Comments
     
    LVL 1

    Expert Comment

    by:hbash
    Looks ok.  What are you looking for?  Is it you need help on launching the details window?
    0
     

    Author Comment

    by:flosoft
    Yes, preferably with the javascript,

    I cannot get the new window code down I think it might go something like...

            If e.CommandName = "ShowDetails" Then
                'Check if pop new window is checked.
                If cbPopDetails.Checked = True Then 'Show details in new widow

              response.redirect("javascript:na_open_window('Details', 'details.aspx', 100, 100, 400, 300, 0, 0, 0, 0, 0);")

              'This preferably will use a javascript pop up, so I can set size, position
              'and scroll, toolbars, etc.
                Else
               'Show details on the same page.

                    Try
                        GetItemDetails(e.Item.Cells(0).Text)

                    Catch ex As Exception
                        lblTitle.Text = "itm command " & ex.Message.ToString
                    Finally
                        '
                    End Try
                End If
            End If


    however this does not seem to work...

    hmmmmm
    0
     

    Author Comment

    by:flosoft
    A note:

    I know I could probably do this if the column was a hyperlink column, but I believe
    I would have to repost the page just to show details if the user did not check the popup box,
    and everything works fine thus far, so the best solution is the following:

    Pops a new window, where I can set size, title, toolbars, scrollbars etc.

    I can pass in the ID of the item clicked, which I am already trapping with the e.Item.Cells(0).Text

    'not a working sample, just a quick note on how it will be.
    ie: popnew(details.aspx?itmID=e.Item.Cells(0).Text)

    Then I grab the needed details from the supplied ID in the details.aspx form load.

    Thanks, just thought I would clear it up a bit.
    0
     
    LVL 2

    Expert Comment

    by:gbajramo
    Use a template column and insert your custom javascript that will open a new window there.
    0
     

    Author Comment

    by:flosoft
    ?? Have not worked much with template columns, could you post an example of adding the needed properties and events? or
    setting up an existing type as a template that would have what I need given then guidelines?
    I do not see a link or commandID property for a template coumn.
    0
     
    LVL 2

    Expert Comment

    by:gbajramo
    Here is an example of how I envoke a javascript confirmaiton on a link using template column. You can easily rework it to call any javascrip you'd need.

                      <asp:templatecolumn headertext="Actions" itemstyle-horizontalalign="Center" headerstyle-horizontalalign="Center">
                            <itemtemplate>
                                  <asp:linkbutton id="btnDelete" runat="server" commandname="Delete"><span onclick="if (confirm('Are you Sure?')){return(true)}else{return(false)};">Delete</span></asp:linkbutton>
                            </itemtemplate>
                      </asp:templatecolumn>
    0
     

    Author Comment

    by:flosoft
    I guess I am not getting your answer, I have code running in the .vb file already working, your proposal is html, I realize I may have to edit some of the
    html, but I am not seeing how I would call this from my .vb files code. I think somehow I may have to add an attribute or something along those lines.

    My datagrid is filled in from a database, the item a user will click on in the grid is the name of a products (from the database).

    This cell has a commandname which is the code you see above. I might not be undestanding how your answer allows for the 2 behaviors based on
    the checkbox's checked status.

    If I am wrong on this please clarify because I am just not seeing it.

    Thanks.
    0
     
    LVL 2

    Expert Comment

    by:gbajramo
    You said below you need help with launching a new window without postback. Is this correct?

    My sample is not exact solution for your problem but illustration of how I interract with javascript form grid. You will need to rework the javascript to check for your checkbox status and pop up the new window. Replace the code in span onclick with call to your window javascript funciton

    onClick = "na_open_window('Details', 'details.aspx', 100, 100, 400, 300, 0, 0, 0, 0, 0);"

    What you call the HTML code is actually asp.net code needed to define a grid template column. Explaining how templated columns work is outside of the scope of this question.
    0
     

    Author Comment

    by:flosoft
    I did not realize my question had a scope. That aside, if all's I wanted was to popup a window I would have used a
    hyperlink column and called the javascript from the link, my question was a bit more than poping up a window which I already can do.
    My question was how to get the behaviors I was looking for based on the check box's value and poping the window based on that value,
    not just poping a window every time it was clicked regardless of the checkbox.
    I do think you might be on the right track however because the itemtemplate is an asplink button, for which I can add
    attributes, then call what I need. (I think). I will post the code as soon as I get it all working.
    0
     
    LVL 2

    Expert Comment

    by:gbajramo
    Well, I'm glad I clarified the scope for you. It seems that you are more interested in arguing than reading and understanding the postings.

    I suggested that:

    1. You can use javascript to check the value of the checkbox on the client side
    2. Again use javascript to pop the window if the value in checkbox value

    Is there something else you are trying to achieve that is not mentioned here?
    0
     

    Author Comment

    by:flosoft
    I am not interested in arguing, and I am sorry if you took it that way,
    I was just under the impression that the person asking a Q determines the "scope" and if needed raises or lowers
    points if the question takes longer to solve.
    The answers you were giving did not match what I was asking, maybe my Q was not worded correctly.
    Thus my comments as I think it was obvious that the code you were posting was not accomlishing what
    I was asking which was yes, poping the window (which your example shows) but this I already could do,
    but what I was asking was hopefully allowing the code I wrote (shown above) to be maintained if possible,
    or I was hoping to see a working  example if another method was to be used.

    Im regards to your last suggestion, I know I have to use some sort of javascript, this I knew before I even asked the
    question in the 1st place, I am not farmiliar enough with linking javascript and asp.net yet,
    so if I was to wire that in I would need better examples or better answers than "just use javascript".

    Please keep in mind I am not trying to be argumentative, but I feel like you had a decent point of direction, but I already
    knew pretty what you said, I am not sure how to wire in the checkbox value and send that to my .vb code behind,
    which is key if you read what I have posted.

    I have a "version" of it working using just an asp link button and adding an attribute to it, If I could figure out how to
    add that code to the grids templatecolumn asp linkbutton, I think I would be all set, but I have not figured out how to
    get at the template in code.

    I have tried grid.items(xx).attribues.add
    when I use grid.attributes.add(the javascript) - this works as well, but for the whole grid, not just the column I am working with.

    I apologize for any frustrations I may have caused you.

    0
     
    LVL 1

    Expert Comment

    by:hbash
    Silly question:  Don't you want to have a "show details column" so that you can associate each request to show details with a some row id whiich you could pass to a piece of c# of vb method to display a form using that row's id to retrieve the details for?  Forgive me if I am missing something here.  It just seems that clicking a column selects the column not a row from a resultset.
    0
     

    Author Comment

    by:flosoft
    What I have is basically a user has a page with a datagrid, which has items in it, pulled from a SQL DB.

    The items have many more fields than can be displayed in the grid and look nice, so I only display a few. When a user clicks on
    the name of an item I want it to popup a controllable window based on a checkbox on the page, the check box's text is
    show details in new window. if the box is checked, a new window needs to open to a page I have created, itemdetails.aspx.

    I already have a details below the grid, but I want them to have the option of this other .aspx page.

    So the process is strait forward:
    User clicks name of item
    If Checkboxpopup.checked then
    "Pop the new window sending in the ID of the item checked.
    else
    'just display on current page
    end if

    as I posted I have some code working, but for a linklabel, I cannot seem to get at a linklabel inside of a datagrid template column to
    add the atribute to it.

    Maybe this is more difficult a question than I first thought so I am uppping the points to 500.
    0
     
    LVL 1

    Expert Comment

    by:hbash
    I understand that part of your program.  You want to show details either on the same page as the grid or on it's own browser
     window based on the checking of a checkbox.
    You could make a template column, and have a little bit of asp.net code build the javascript that the selecting of that row including
    key field info as a function parameter.

    But the method of selection seems to be the question here, too.  You don't want to use a link button.  What visual method do
    you intend for your user(s) to use to select the row for detail?
    0
     
    LVL 2

    Expert Comment

    by:gbajramo
    This isn't so difficult but you don't seem to have the hang of the web development to fully understand the suggestions being offered. The way you are currently popping the window will not work for what you are trying to achieve. I'm not going to go into explaining this again since I already gave you the example on how. It would be best if I just write the code for you. Please provide your entire grid definition from HTML view along with the entire databinding code.
    0
     

    Author Comment

    by:flosoft
    It would seem it is difficult and this has been the only frustrating experience I have had on EE since I became a member in 1998.
    I have laid out the question as good as I can. A suggestion with a line of code to do something completely different, the statement
    "use javascript" if this was a common type response on here I could answer a thousand questions a day for example someone asking how
    to implement a multithreaded server in vb.net, my answer could be "use sockets" and then the suggestions that I am
    incapable of understanding what you are posting is not helping the problem, obviously I cannot explain this clear enough to just get a
    decent example, I understand this well enough to eventually figure out how to get the value I need and acomplisg the task,
    so  I wil figure this one out myself.
    0
     

    Author Comment

    by:flosoft
    To hbash, please note that I am not intending to dismiss your questions or suggestions, and to gbajramo I do appreciate
    you attempting to help, but either the question is harder or I am not able to phrase it correctly which is why I do not feel I
    am going to get an answer. I do appreciate your time on this, but short of some sort of working example, that leaves me
    having to research online or in books to solve the problem and this I could have done with using EE.

    I did not want to leave the question with just my last comment as I do appreciate the efforts, but I just do not see any solutions
    coming on here for this q at this point.

    Thanks again.
    0
     

    Author Comment

    by:flosoft
    typo - could have done {without} using EE.
    0
     
    LVL 1

    Expert Comment

    by:hbash

    You say "I want to display ... when the user clicks on the name of the item" (called the heading in grid-ese).  That selects an entire column.
    Don't you want to select the row for which you want the details displayed?  I think that biggest problem here is that you have not
    provided this level of detail describing what visually is going on for the user.  You say check or don't check a checkbox, that's fine.
    But how does selecting an item name chose a data row?  What am I missing here, or you omitting from the description of your GUI?
    0
     

    Author Comment

    by:flosoft
    Ok,
    I will try to explain it again with better detail.

    I have a datagrid, which is it loaded with products, the 1st column contains the products ID the 2nd the prodcut the product name,
    this is the one where I want the action to take place.
    This column is currently a template column via gbajramo suggestion, however it could be a button or hyperlink column. So just
    a basic datagrid with products in it.
    On the webform I also have an asp checkbox. I do not need to select the full row. The webform I am trying to get to work right in this
    question just needs a product ID to display the details.

    So the user is looking at the datagrid as explained above and has a choice of checking the checkbox or not, if the checkbox is not
    checked, I have this handled already, however if they check the box, then I am trying to open a click on the product name link
    in a new window, passing the webform via the URL the product ID. ie:webformDetails.aspx?ProdcutID=

    I hope this clears it up.




    0
     
    LVL 2

    Expert Comment

    by:gbajramo
    What are you asking for? Working code or explanation? If you need working code, post your code. If you need explanation on how to do this, and you can do it yourself, read my postings.
    0
     

    Author Comment

    by:flosoft
    This is the Grid and Checkbox code.
    <ASP:DATAGRID id="dgView" runat="server" Width="100%" Font-Size="XX-Small" Font-Names="Tahoma"
    ALLOWSORTING="True" Height="184px" BorderColor="YellowGreen" BorderStyle="Solid" BorderWidth="1px"
    BackColor="White" CellPadding="3" AutoGenerateColumns="False" SHOWFOOTER="True" ALLOWPAGING="True">
    <SELECTEDITEMSTYLE FONT-BOLD="True" FORECOLOR="#F7F7F7" BACKCOLOR="#738A9C"></SELECTEDITEMSTYLE>
    <ALTERNATINGITEMSTYLE FORECOLOR="Black" BACKCOLOR="Gainsboro"></ALTERNATINGITEMSTYLE>
    <ITEMSTYLE FORECOLOR="DimGray" BACKCOLOR="WhiteSmoke"></ITEMSTYLE>
    <HEADERSTYLE FONT-BOLD="True" HORIZONTALALIGN="Center" BORDERWIDTH="1px" FORECOLOR="#F7F7F7"
    BORDERCOLOR="Black" BACKCOLOR="YellowGreen"></HEADERSTYLE>
    <FOOTERSTYLE FONT-SIZE="XX-Small" FONT-NAMES="Verdana" FORECOLOR="#4A3C8C" BACKCOLOR="YellowGreen"></FOOTERSTYLE>
    <COLUMNS>
    <ASP:BOUNDCOLUMN Visible="False" DataField="ID"></ASP:BOUNDCOLUMN>
    <ASP:TEMPLATECOLUMN HeaderText="Product">
    <ITEMTEMPLATE>
    <asp:LinkButton runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.w_Name") %>'
    CommandName="GetDetails" CausesValidation="false">
    </ASP:LINKBUTTON>
    </ITEMTEMPLATE>
    </ASP:TEMPLATECOLUMN>
    <ASP:HYPERLINKCOLUMN Text="Visit" Target="New" DataNavigateUrlField="w_URL" HeaderText="Web Link">
    <HEADERSTYLE WIDTH="60px"></HEADERSTYLE>
    <ITEMSTYLE HORIZONTALALIGN="Center"></ITEMSTYLE>
    </ASP:HYPERLINKCOLUMN>
    <ASP:BOUNDCOLUMN DataField="w_Store" HeaderText="Website or Store"></ASP:BOUNDCOLUMN>
    <ASP:BOUNDCOLUMN DataField="w_LowPrice" HeaderText="Est Price" DataFormatString="$ {0:C}"></ASP:BOUNDCOLUMN>
    </COLUMNS>
    <PAGERSTYLE FONT-SIZE="X-Small" HORIZONTALALIGN="Right" FORECOLOR="#4A3C8C" BACKCOLOR="Gainsboro"
    PAGEBUTTONCOUNT="5" MODE="NumericPages"></PAGERSTYLE>
    </ASP:DATAGRID>
    <P align="center"><ASP:CHECKBOX id="cbPop" runat="server" AUTOPOSTBACK="True" Text="Open Details in new Window">
    </ASP:CHECKBOX></P>

    This is my .vb code behind code for the command.
            If e.CommandName = "GetDetails" Then
                Try
                    If cbPop.Checked = True Then
                    This is where I need to show the details in the new window if possible.
                    Else
                    'Show details on same page
                        GetDetails(e.item.cells(0).text)
                    End If
                Catch ex As Exception
                    lblDetails.Text = "An Error Occured " & ex.Message.ToString
                Finally
                    '
                End Try

            End If

    above I listed some code for poping a window using javascript.
    I also have this vb code to pop a new window from the code behind, but I have not been able to get this to work with the
    grid either, but I will post it anyways.

        Public Shared Sub PopWindow(ByVal opener As System.Web.UI.WebControls.WebControl, ByVal PagePath As String, ByVal windowName As String, ByVal width As Integer, ByVal height As Integer)
            Dim clientScript As String
            Dim windowAttribs As String

            'Building Client side window attributes with width and height.
            'Also the the window will be positioned to the middle of the screen
            windowAttribs = "width=" & width & "px," & _
                      "height=" & height & "px," & _
                      "left='+((screen.width -" & width & ") / 2)+'," & _
                      "top='+ (screen.height - " & height & ") / 2+'"

            'Building the client script - window.open, with additional parameters
            clientScript = "window.open('" & PagePath & "','" & windowName & "','" & windowAttribs & "');return false;"
            'register the script to the clientside click event of 'opener' control
            opener.Attributes.Add("onClick", clientScript)
        End Sub

    That's about all I think of to post relevant to the problem.

    Thanks again.
    0
     
    LVL 1

    Expert Comment

    by:hbash
    Did you put a break point and hit it in you vb code behind handler for the grids ItemCommand event?  
    It needs to be associated with your grid or you'll just never get there.  Sorry if this is an
    obvious suggestion, but it is possible.
    0
     

    Author Comment

    by:flosoft
    I seem to not be able to explain this well enough, and I found an acceptable work around, so if a moderator views this, please delete this question.

    Thanks
    0
     

    Author Comment

    by:flosoft
    I figured out a way to work with this, which goes something like this:
    Please keep in mind I am just typing this so cut and paste will not work.

    1st - make the item you want to work with a template linklabel

    in the GridFill

    Use the dataset

    Dim i as integer

    For i = 0 to dataset.tables("Table").rows.count - 1
    if checkbox.checked = true
    linklabel.attributes.add(add the attributes)
    else
    linklabel.attributes.remove(remove if not checked)
    end if
    next

    again if a moderator finds this listing, please either close it if you feel it was correctly answered by any of the poster,
    or delete and return points.

    Thanks
    0
     

    Accepted Solution

    by:
    PAQed with points refunded (500)

    modulo
    Community Support Moderator
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    This article will show, step by step, how to integrate R code into a R Sweave document
    A short article about problems I had with the new location API and permissions in Marshmallow
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    913 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

    17 Experts available now in Live!

    Get 1:1 Help Now