Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

When click edit inside Gridview, No Update or Cancel buttons shows

$
0
0

Hi,

The intention is to create a gridview that has edit/delete, I am able to reach this far,
 but when I click Edit, the gridview does not show Update , cancel buttons,
 I appreciate your help looking at the code and correcting what I am doing wrong.

Thanks

bob

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
 DataKeyNames="contactId"
    OnRowDeleting="GridView1_RowDeleting"
 OnRowEditing="GridView1_RowEditing"
 OnRowCommand="GridView1_RowCommand"
     OnPageIndexChanging="GridView1_PageIndexChanging"
 OnRowCancelingEdit="GridView1_RowCancelingEdit"
    OnRowUpdating="GridView1_RowUpdating">
    <Columns>
     <asp:ButtonField ButtonType="Button" Text="Select" HeaderText="ButtonField" CommandName="Select" />
      <asp:ButtonField ButtonType="Button" Text="Edit" HeaderText="ButtonField" CommandName="Edit" />
      <asp:ButtonField ButtonType="Button" Text="Delete" HeaderText="ButtonField" CommandName="Delete" />
      <asp:TemplateField HeaderText="contactId">
            <ItemTemplate>
                <asp:Label ID="lblcontactId" runat="server" Text='<%# Bind("contactId") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtcontactId" runat="server" Text='<%# Bind("contactId") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="name">
            <ItemTemplate>
            <asp:LinkButton ID="LbContactDetail" runat="server" CommandArgument='<%#Eval("contactId")%>'
                    CommandName="contactDetail" Text='<%# Bind("name") %>'>
                </asp:LinkButton>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtname" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="note">
            <ItemTemplate>
                <asp:Label ID="lblnote" runat="server" Text='<%# Bind("note") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtnote" runat="server" Text='<%# Bind("note") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

codebehind
---------
        protected void Page_Load(object sender, EventArgs e)
        {
            string sql = "select contactId,name,dob,sal,countryid,active,note from contact";
            DataTable dt = GetDt(sql);
            GridView1.DataSource = dt; 
            GridView1.DataBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            lblMsg.Text = "Source : GridView1_RowEditing <BR>";
            lblMsg.Text += "to get row index being editing";
            GridView1.EditIndex = e.NewEditIndex;// get teh row index that is being editing
            GridView1.DataBind(); // must be to sync rows with index
        }

public void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    GridView1.DataBind();
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    GridViewRow selectedRow = GridView1.Rows[e.RowIndex];
    Label lblContactId = selectedRow.FindControl("lblcontactId") as Label;
    lblMsg.Text = " you are in GridView1_RowDeleting to delete: " + lblContactId.Text.ToString();
    // to delete
    string sql = "delete contact where contactid = @contactid";
    Dictionary<string, object> whereClause = new Dictionary<string, object>();
    whereClause.Add("@contactId", int.Parse(lblContactId.Text.ToString()));
    int recAffected = new BLDs(Global.DB_CONN_STRfTree).AddEditDeleteViaSql(sql, whereClause);
    if (recAffected >= 1)
    {
        lblMsg.Text = "Sucess";
        Page_Load(null, null); // is there a better way than this ????

    }
    else
        lblMsg.Text = "Fail";
}

// is this corrected.... This code is never fired ????
public void GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
    GridViewRow gvr = ((GridView)sender).Rows[e.RowIndex];  // get row
    string pk = ((Label)gvr.FindControl("contactID")).Text; // get pk
    string name = ((TextBox)gvr.FindControl("name")).Text;
     

}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();
}


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    // If multiple ButtonField column fields are used, use the
    // CommandName property to determine which button was clicked.
    if (e.CommandName == "Select")
    {
        // Convert the row index stored in the CommandArgument
        // property to an Integer.
        // e.CommandArgument is the index itself
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = GridView1.Rows[index];

        // ONE approach via findcontrol to get the values from the row
        Label lblContactId = selectedRow.FindControl("lblcontactId") as Label;
        LinkButton LbName = selectedRow.FindControl("LbContactDetail") as LinkButton;
        Label lblNote = selectedRow.FindControl("lblnote") as Label;
        string strContactId = lblContactId.Text.ToString();
        string strName = LbName.Text.ToString();
        string strNote = lblNote.Text.ToString();
        lblMsg.Text = "Source : GridView1_RowCommand - Select<BR>";
        lblMsg.Text += "Selected Index: " + index + "<BR>";
        lblMsg.Text += "Selected Contact ID: " + strContactId + "<BR>";
        lblMsg.Text += "Selected Name: " + strName + "<BR>Selected Note:" + strNote + "<BR>";
        lblMsg.Text += "Command Name: " + e.CommandName.ToString();

    }
    if (e.CommandName == "Edit")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = GridView1.Rows[index];
        Label lblContactId = selectedRow.FindControl("lblcontactId") as Label;
        LinkButton LbName = selectedRow.FindControl("LbContactDetail") as LinkButton;
        Label lblNote = selectedRow.FindControl("lblnote") as Label;
        string strContactId = lblContactId.Text.ToString();
        string strName = LbName.Text.ToString();
        string strNote = lblNote.Text.ToString();

        // Either create an input form to populate the form for edit/update or
       //make edit inside the grid view.... not working
    }
    if (e.CommandName == "Delete") // using ButtonField
    {
        int index = Convert.ToInt32(e.CommandArgument); // to do if index is >=0
        GridViewRow selectedRow = GridView1.Rows[index];
        Label lblContactId = selectedRow.FindControl("lblcontactId") as Label;
        string strContactId = lblContactId.Text.ToString();
        // call Delete()   and pass contactId
    }
}

 


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>