Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 ASP.NET4.0
Hi,
 
I am using user control for my project.
 
User control has Insert and Update operation.
 
I have written Add/Update in the btnSave_Click event, when i add new item Button click event fire and works fine.and Inserted record i am showing in grid view.
 
on Update Operation i click on edit button which in gridview and binding all values to related text box, and modifying data and click on save button is not firing.
 
what might be the problem, ??
 
Please Help me
 
Here Is my code
 
User Control
<table class="well">
    <tr>
        <td>
            <asp:Label ID="lblSiteNane" runat="server" Text="Site Name:" CssClass="hideextra"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtSiteName" runat="server" Style="height: 16px;"></asp:TextBox>
            <br />
            <asp:RequiredFieldValidator ID="reqSiteName" runat="server" ErrorMessage="Please Enter Site Name"
                ControlToValidate="txtSiteName" Display="Dynamic" CssClass="alert-error"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr runat="server" id="Status" visible="false">
        <td>
            <asp:Label ID="lblStatus" runat="server" Text="Active Status:" CssClass="hideextra"></asp:Label>
        </td>
        <td>
            <asp:CheckBox ID="ckbSiteActive" runat="server" />
        </td>
        <td>
            <asp:HiddenField ID="hiddenId" runat="Server" />
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="btn btn-success" OnClick="btnSave_Click"
                EnableViewState="true" />
            <asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="btn btn-danger" />
        </td>
    </tr>
</table>
<br />
<table>
    <tr>
        <td>
            <asp:Panel ID="Panel1" runat="server" ScrollBars="Auto" HorizontalAlign="Center">
                <center>
                    <div>
                        <asp:GridView ID="grdSites" runat="server" Width="250px" AutoGenerateColumns="False"
                            DataKeyNames="RequestSiteID" HeaderStyle-BackColor="#575893" HeaderStyle-ForeColor="White"
                            EditRowStyle-HorizontalAlign="Center" AlternatingRowStyle-BackColor="#F8FDFF"
                            EmptyDataText="No Request Site Exists">
                            <Columns>
                                <asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkinactive" runat="server" Text="Edit" OnCommand="lnkEdit" CssClass="fblueDarkfbold"
                                            ForeColor="Blue" Font-Bold="false" CommandArgument='<%# Eval("RequestSiteID")%>'
                                            CausesValidation="false" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="User Name" ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <asp:Label ID="lblSiteName" runat="server" Text='<%#Eval("RequestSiteName") %>' CssClass="fblueDarkfbold"></asp:Label>
                                        <asp:Label ID="lblUserId" runat="server" Text='<%#Eval("RequestSiteID") %>' Visible="false"
                                            CssClass="fblueDarkfbold"></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Is Active" ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <asp:CheckBox ID="MyCheckBox" runat="server" Checked='<%# Bind("IsActive") %>' Enabled="false" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </div>
                </center>
            </asp:Panel>
        </td>
    </tr>
</table>
User Control Code Behind
protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      BindDataToGrid();
   }
   lblMessage.Visible = false;
   lblError.Visible = false;
}
 
protected void BindDataToGrid()
{
   List<requestsite> _requestSite = _requestSiteBO.GetRequestSiteDetails(false);
   grdSites.DataSource = _requestSite;
   grdSites.DataBind();
   ViewState["RequestSite"] = _requestSite;
}
 
protected void lnkEdit(Object sender, CommandEventArgs e)
{
   try
   {
      int userId = Int32.Parse(e.CommandArgument.ToString());
      String newText = txtSiteName.Text.Trim();
      RequestSite siteDetail = GetSingleData(userId);
      txtSiteName.Text = siteDetail.RequestSiteName;
      ckbSiteActive.Checked = siteDetail.ISActive;
      hiddenId.Value = Convert.ToString(siteDetail.RequestSiteID);
      Status.Visible = true;
      // btnSave.Text = "Update";
   }
   catch (SqlException ex)
   {
      throw ex;
   }
   catch (Exception ex)
   {
      throw ex;
   }
}
 
private RequestSite GetSingleData(Int32 userId)
{
   List<requestsite> _requestSite = (List<requestsite>)ViewState["RequestSite"];
   var existingData = _requestSite.Where(c => c.RequestSiteID == userId).FirstOrDefault();
   RequestSite _requestSiteDetails = new RequestSite();
   _requestSiteDetails.RequestSiteID = existingData.RequestSiteID;
   _requestSiteDetails.RequestSiteName = existingData.RequestSiteName;
   _requestSiteDetails.ISActive = existingData.ISActive;
   return _requestSiteDetails;
}
 
public void btnSave_Click(object sender, EventArgs e)
{
   int Sucess = 0;
   RequestSite _requestSite = new RequestSite();
   if (btnSave.Text == "Save")
   {
      _requestSite.RequestSiteName = txtSiteName.Text.Trim();
      _requestSite.ISActive = true;
      Sucess = _requestSiteBO.AddRequestSite(_requestSite);
   }
   else if (btnSave.Text == "Update")
   {
      _requestSite.RequestSiteID = Convert.ToInt32(hiddenId.Value);
      _requestSite.RequestSiteName = txtSiteName.Text.Trim();
      _requestSite.ISActive = true;
      Sucess = _requestSiteBO.AddRequestSite(_requestSite);
   }
   if (Sucess == 1)
   {
      lblMessage.Text = ConstantStrings.InsertSuccessMessage;
      lblMessage.Visible = true;
      txtSiteName.Text = "";
   }
   else
   {
      lblError.Text = ConstantStrings.InsertErrorMessage;
      lblError.Visible = true;
   }
}
 
Thanks and regards
 
Vishwa
Posted 6-Jan-13 18:48pm
VishwaKL4.1K
Edited 7-Jan-13 5:00am
v4
Comments
prashant patil 4987 at 6-Jan-13 23:53pm
   
can you post your code???????????
VishwaKL at 6-Jan-13 23:57pm
   
Prashant i have posted my code, can suggest me the solution?
Christian Graus at 7-Jan-13 1:24am
   
I tried to help but you stopped answering. You need to explain where you think the text of the button changes, it's not in any code you posted. I assume that missing code, is the issue
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Sorry if i m wrong,
 
Can you make sure you are calling different methods while insert & update.
 
from your code i can find that you are calling same methods while update and insert
 
" Sucess = _requestSiteBO.AddRequestSite(_requestSite); )"
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Thanks guys
 
I got the solution.
 
WE have to use Validation group for the UI, that cleared my problem,
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.150326.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100