Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi every one,
I face a problem in confirmation Subfor ButtonField in gridview (this my main problem).

I used to use templatefield in gridview for each row & for confirmation, I use "onclientclick" & it works very well beause user can not do any event if he did not click Ok or cancel in other word it is required.

I have now a girdview & I needed to use rowspan & that does not work if I converted column to templatefield.

My gridview columns are: Exp_ID, Experiment, Element_ID, Element, Quantity, ubdate_button & delete_button.

Any experiment has many elements & any element must has a quantity, so I made rowspan for Exp_Id, Experiment & the buttons because I will edit or delete 1 experiment.

My problem was in rowspan & I solved by using BoundField & ButtonField (without converting to templatefield) and that enabled me to do it.
By row.Cells(row_number).text I can get Exp_ID(PK) for edit.

But my current problem is in deleting experiment with confirmation

I have 50% of the solution by using MsgBox but the problem is in prevent user to make any event except 2 options of confirmation ok or cancel (user can make any event without select MsgBox options)

I could not use onclientclick for ButtonField does not have a public property called "onclientclick"

The avilable solutios are:
1-Enabling using rowspan fro templatefield.
2-Prevent user to make any event except confirmation options

I hope the problem is well described

Please find code behind for rowsspan & source code for gridview

gridview source code:

ASP.NET
<asp:GridView ID="gv_exp" runat="server" AutoGenerateColumns="False" CssClass="mGrid" Width="45%" EmptyDataText="no data found" BorderWidth="1px">
                     <AlternatingRowStyle BackColor="#DCDCDC" />
                    <Columns>
                          <asp:BoundField DataField="Exp_ID" HeaderText="ID" ItemStyle-BorderWidth="1px" ItemStyle-BorderColor="Black" >
                          <ItemStyle BorderColor="Black" BorderWidth="1px" />
                          </asp:BoundField>
                          <asp:BoundField DataField="Exp_Name" HeaderText="Experiment" ItemStyle-BorderWidth="1px" ItemStyle-BorderColor="Black" >
                          <ItemStyle BorderColor="Black" BorderWidth="1px" />
                          </asp:BoundField>
                          <asp:BoundField DataField="element_id" HeaderText="Element ID" ItemStyle-BorderWidth="1px" ItemStyle-BorderColor="Black" >
                          <ItemStyle BorderColor="Black" BorderWidth="1px" />
                          </asp:BoundField>
                          <asp:BoundField DataField="element_name" HeaderText="Element" ItemStyle-BorderWidth="1px" ItemStyle-BorderColor="Black" >
                          <ItemStyle BorderColor="Black" BorderWidth="1px" />
                          </asp:BoundField>
                          <asp:BoundField DataField="quantity" HeaderText="Quantity" ItemStyle-BorderWidth="1px" ItemStyle-BorderColor="Black" >
                          <ItemStyle BorderColor="Black" BorderWidth="1px" />
                          </asp:BoundField>
                          <asp:ButtonField Text="ubtn" ButtonType="Image" ImageUrl="images/b_edit.png" CommandName="u123" ItemStyle-BorderWidth="1px" ItemStyle-BorderColor="Black" >
                          <ItemStyle BorderColor="Black" BorderWidth="1px" />
                          </asp:ButtonField>
                          <asp:ButtonField Text="dbtn" ButtonType="Image" ImageUrl="images/Delete.gif" CommandName="d123" ItemStyle-BorderWidth="1px" ItemStyle-BorderColor="Black" >
                          <ItemStyle BorderColor="Black" BorderWidth="1px" />
                          </asp:ButtonField>
                    </Columns>
                </asp:GridView>


What I have tried:

Code behind for buttons operatios(MsgBox as comment)

VB
Protected Sub gv_exp_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gv_exp.RowCommand

       Dim rowindex As Integer = Convert.ToInt32(e.CommandArgument)
       Dim row As GridViewRow = gv_exp.Rows(rowindex)

       If e.CommandName = "u123" Then

           lbmsg.Text = ""
           tr_add_new.Visible = True
           tr_add_new_table.Visible = True
           tr_cancel.Visible = True
           insert.Visible = False
           clear.Visible = False
           update.Visible = True
           exp_id = row.Cells(0).Text
           gv_eles_items(exp_id, "n")
           dt_gv_eles = userr.get_elements(exp_id, "y")
           If dt_gv_eles.PrimaryKey.Length = 0 Then
               dt_gv_eles.PrimaryKey = New DataColumn() {dt_gv_eles.Columns("ID")}
           End If
           i_gv_eles.DataSource = dt_gv_eles
           i_gv_eles.DataBind()

           i_exp_txt.Text = row.Cells(1).Text
           i_ele_quan.Text = ""

       ElseIf e.CommandName = "d123" Then

           tr_add_new_table.Visible = False
           tr_cancel.Visible = False
           tr_add_new.Visible = True


           ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('You clicked YES!')", True)


           'Dim a = MsgBox("Are you sure you want to delete selected experiment", MsgBoxStyle.OkCancel, "")

           'If a = MsgBoxResult.Ok Then

           '    operation = "Delete"

           '    i = userr.delete_experiment(row.Cells(0).Text, userid, role, operation, object1)

           '    If i = -1 Then
           '        lbmsg.Text = "there is mistake"
           '    Else

           '        lbmsg.Text = "experiment deleted successfully"
           '        gvdata()

           '    End If
           'Else

           'End If


       End If
   End Sub


code behind for rowspan

VB
Protected Sub gv_exp_DataBound(sender As Object, e As EventArgs) Handles gv_exp.DataBound


      For i As Integer = gv_exp.Rows.Count - 1 To 1 Step -1
          Dim row As GridViewRow = gv_exp.Rows(i)
          Dim previousRow As GridViewRow = gv_exp.Rows(i - 1)

          If row.Cells(1).Text = previousRow.Cells(1).Text Then

              If previousRow.Cells(1).RowSpan = 0 Then

                  If row.Cells(1).RowSpan = 0 Then

                      previousRow.Cells(1).RowSpan += 2
                      previousRow.Cells(0).RowSpan += 2
                      previousRow.Cells(5).RowSpan += 2
                      previousRow.Cells(6).RowSpan += 2
                  Else
                      previousRow.Cells(1).RowSpan = row.Cells(1).RowSpan + 1
                      previousRow.Cells(0).RowSpan = row.Cells(1).RowSpan + 1
                      previousRow.Cells(5).RowSpan = row.Cells(5).RowSpan + 1
                      previousRow.Cells(6).RowSpan = row.Cells(6).RowSpan + 1
                  End If

                  row.Cells(1).Visible = False
                  row.Cells(0).Visible = False
                  row.Cells(5).Visible = False
                  row.Cells(6).Visible = False
              End If
          End If


      Next
  End Sub
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900