Edit and Row command in a grid view

Hello,

I'm having a row command and edit in a grid. When I click the edit it triggers the same action as the rowcommand


 Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand


What should I do about the edit command ?
LVL 2
navinbabuAsked:
Who is Participating?
 
intlaqaCommented:
Hi,

This is your custom command, right?

<asp:ButtonField  Text="Allot to  Student"  />

If yes, please add the CommandName property, to be like this:

<asp:ButtonField CommandName="Allot"  Text="Allot to  Student"  />

Then, make your  GridView1_RowCommand like this:

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "Allot" Then
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = GridView1.Rows(index)
            Response.Redirect("Assign.aspx?deptid=" + row.Cells(0).Text)
        End If
    End Sub

I found that you are just redirecting the user to another page (assign.aspx) , you can use a HyperLinkField instead of the custom ButtonField, so you may remove:

<asp:ButtonField  Text="Allot to  Student"  />

And replace it with:

<asp:HyperLinkField Text="Allot to  Student" DataNavigateUrlFields="Deptid" DataNavigateUrlFormatString="Assign.aspx?deptid={0}" />
0
 
intlaqaCommented:
Hi,

What are you doing in the Row Command? anyway start the code in your RowCommand with something like:

if(e.CommandName = "MyCustomCommand")
{
// your code here
}

And if you have custom buttons in your rows, just give them a CommandName, for example CommandName="MyCustomCommand".

If you do not get it, please show me both your ASPX Grid View code and your RowCommand Codebehind code.
0
 
navinbabuAuthor Commented:
This is my Grid

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                    BackColor="White" BorderColor="#CC9966" BorderStyle="None" BordrWidth="1px"
                    CellPadding="4" DataSourceID="SqlDataSource3" AllowPaging="True"
                       DataKeyNames="DeptId" OnItemDataBound="Item_Bound"
                       EnableModelValidation="True">
                    <RowStyle BackColor="White" ForeColor="#330099" />
                    <Columns>
                        <asp:BoundField DataField="DeptId"  HeaderText="DeptId" Visible="true" />
                             <asp:BoundField DataField="DeptName" HeaderText="Name"
                            SortExpression="DeptName" />
                        <asp:BoundField DataField="Course" HeaderText="Course"
                            SortExpression="Course" />
                        <asp:BoundField DataField="GPA" HeaderText="GPA"
                            SortExpression="GPA" />
                        <asp:BoundField DataField="GRE_V" HeaderText="GRE Verbal"
                            SortExpression="GRE_V" />
                        <asp:BoundField DataField="GRE_Q" HeaderText="GRE Quant"
                            SortExpression="GRE_Q" />
                        <asp:BoundField DataField="GRE_AWA" HeaderText="GRE AWA"
                            SortExpression="GRE_AWA" />
                        <asp:BoundField DataField="T_R" HeaderText="TOEFL Reading"
                            SortExpression="T_R" />
                        <asp:BoundField DataField="T_W" HeaderText="TOEFL Writting"
                            SortExpression="T_W" />
                        <asp:BoundField DataField="T_L" HeaderText="TOEFL Listening"
                            SortExpression="T_L" />
                        <asp:BoundField DataField="T_S" HeaderText="TOEFL Speaking"
                            SortExpression="T_S" />
                        <asp:BoundField DataField="IELTS" HeaderText="IELTS"
                            SortExpression="IELTS" />
                        <asp:BoundField DataField="GMAT" HeaderText="GMAT" SortExpression="GMAT" />
                        <asp:CommandField ShowEditButton="True" />
                        <asp:ButtonField  Text="Allot to  Student"  />
                    </Columns>
                    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                </asp:GridView>


Here is the backend Code :


    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim row As GridViewRow = GridView1.Rows(index)
        Response.Redirect("Assign.aspx?deptid=" + row.Cells(0).Text)
    End Sub
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
navinbabuAuthor Commented:
Here is the DataSource

<asp:SqlDataSource ID="SqlDataSource3" runat="server"
                       ConnectionString="<%$ ConnectionStrings:db %>"
                       SelectCommand="SELECT * FROM [Departments] WHERE (([UnivId] = @UnivId) AND ([DeptName] = @DeptName) AND ([Course] = @Course))">
                       <SelectParameters>
                           <asp:QueryStringParameter Name="UnivId" QueryStringField="id" />
                           <asp:ControlParameter ControlID="ddl_dept" Name="DeptName"
                               PropertyName="SelectedValue" Type="String" />
                           <asp:ControlParameter ControlID="ddl_course" Name="Course"
                               PropertyName="SelectedValue" Type="String" />
                       </SelectParameters>
                   </asp:SqlDataSource>

and Also can I avoid using the DeptId which I'm including as a column . Which I'm calling in the row command .

1) I tried assigning the DeptId as datakey of the grid . And tried calling it back in the server side by

GridView1.SelectedValue

I dont get any value.

2) Here I added a edit command button and it also triggers the same action as the my custom command. How can I correct this ?
0
 
navinbabuAuthor Commented:
Thanks intlaga it worked like a charm.

I recently converted a asp.net website to web application. Now every change I make in the Visual studio 2010 are not visible in the running local host .

For example.

The above problem page name is University.aspx and server side code University.aspx.vb

I right click view in browser for the University.aspx which triggers a local host and open a page in browser etc.

Now when I used asp.net website I used to make the change in the visual studio and just refresh the page in my browser to see the new changes but after converting to the web application to see the changes I made in the VS. I need to again right click the file for View in browser. Which is very idiotic.

How does these things work usually ? How do people observe changes in industry ?
 
0
 
intlaqaCommented:
Hi navinbabu, you're welcome.

Yes, working in a Web Application project is different, you have to Rebuild the project.. etc after making the changes in classes.. etc.

While the page in the browser (After clicking on View in Browser) you can play with the ASPX by changing styles, adding <div>'s .. etc and just save the page and refresh the browser to see the changes.

But when you change the code-behind or some classes, add reference to something.. etc, you have to build it first, I usually hit CTRL+SHIFT+B to build the project and go to the browser and refresh to see the changes.
0
 
navinbabuAuthor Commented:
Any work around ? My habit of checking everything in the browser make this quite difficult and I have a cloud service attached to the solution so building the app takes a lot of time.
0
 
navinbabuAuthor Commented:
And again can I convert the application back to the website ?
0
 
intlaqaCommented:
Hi,

I am stuck with some old projects and cannot take the risk to move to VS 2010 soon. I am sorry I can not help in this.

Converting Web Application project back to Website is not easy in current versions of VS. But perhaps VS 2010 has something new features that makes this kind of convertion easy.
0
 
navinbabuAuthor Commented:
ok . But One thing I find very useful in vs 2010 is auto complete for the table name and even their columns which is quite useful
0
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.

All Courses

From novice to tech pro — start learning today.