0

Hi I am trying to update some database values with a simple form with Telerik RadTextBoxes. I can set the text value no problem but when it comes to saving on button click the values do not get updated. My code to set the textbox is in the Page Load function however I am checking if(!IsPostBack)

Here is my html:

<div style="width:95%;">
<div style=" width:100%;float:left; background-color:#fff; border:1px solid #d1d2d3; margin-top:25px; padding:15px; -webkit-box-shadow: inset 1px 0px 5px 0px rgba(50, 50, 50, 0.10); -moz-box-shadow:    inset 1px 0px 5px 0px rgba(50, 50, 50, 0.10); box-shadow: inset 1px 0px 5px 0px rgba(50, 50, 50, 0.10);">
    <h2 style="float:left; width:50%;"><asp:Literal ID="litTitle" runat="server" Text="Add A New Zone" /></h2>
    <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Width="100%">
        <telerik:RadNotification runat="server" id="notifyError" Position="Center" Width="330" Height="130" EnableRoundedCorners="true" EnableShadow="true" Style="z-index: 35000"></telerik:RadNotification>

        <div style="float:right;margin-right:10px;margin-left:5px;"><telerik:RadButton ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" /></div><div style="float:right;margin-right:5px;"><telerik:RadButton ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" /></div><div style="float:right;margin-right:5px;"><telerik:RadButton ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" OnClientClicked="confirm('Are you sure you wish to delete this zone?');" /></div>
        <div class="clear" style="clear:both;height:25px;"></div>
        <div style="width:100%;">
            <div style="float:left;">
                <div style="float:left; margin-left:10px; margin-right:5px; width:200px;">Zone Code:</div>
                <div style="float:left; margin-left:5px; margin-right:10px;"><telerik:RadTextBox ID="txtZoneName" runat="server" CausesValidation="false" Width="200"></telerik:RadTextBox></div>
            </div>
            <div class="clear" style="height:35px;clear:both;"></div>
            <div style="float:left;">
                <div style="float:left; margin-left:10px; margin-right:5px; width:200px;">Name:</div>
                <div style="float:left; margin-left:5px; margin-right:10px;"><telerik:RadTextBox ID="txtZoneCode" runat="server" CausesValidation="false" Width="200"></telerik:RadTextBox></div>
            </div>
            <div class="clear" style="height:35px;clear:both;"></div>
            <div>
                <div style="float:left; margin-left:10px; margin-right:5px; width:200px;">Monthly Book Page Goal:</div>
                <div style="float:left; margin-left:5px; margin-right:10px;"><telerik:RadTextBox ID="txtZoneBookGoal" runat="server" CausesValidation="false" Width="200"></telerik:RadTextBox></div>
            </div>
            <div class="clear" style="height:45px;clear:both;"></div>
            <div style="width:250px; margin:0 auto;">
                <div style="float:left;"><telerik:RadButton ID="btnCreate" runat="server" Text="Save" OnClick="btnCreate_Click" /></div>
            </div>
        </div>
    </telerik:RadAjaxPanel>
</div>
</div>

Here is my c#:

protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            if(Request.Params["z"] != null)
            {
                Zones z = Zones.GetByID(Utils.GetLong(Request.Params["z"]));

                this.txtZoneCode.Text = z.Code;
                this.txtZoneName.Text = z.Name;
                this.txtZoneBookGoal.Text = Utils.GetString(z.BookGoal);
                this.litTitle.Text = "Edit Zone";
            }
        }
    }

protected void btnSave_Click(object sender, EventArgs e)
    {
        bool valid = true;
        string error = "";

        if (this.txtZoneCode.Text == "")
        {
            valid = false;
            error += "- Code<br/>";
        }

        if (this.txtZoneName.Text == "")
        {
            valid = false;
            error += "- Name<br/>";
        }

        if (valid)
        {
            if (Request.Params["z"] != null)
            {
                Zones z = Zones.GetByID(Utils.GetLong(Request.Params["z"]));

                z.Code = this.txtZoneCode.Text;
                z.Name = this.txtZoneName.Text;
                z.BookGoal = Utils.GetInt(this.txtZoneBookGoal.Text);

                z.Save();

                if (z.ZoneID > 0)
                {
                    Response.Redirect("ManageZones.aspx?v=1", true);
                }
                else
                {
                    this.notifyError.Title = "Zone update was not successfull!";
                    this.notifyError.Text = "There was an error saving your zone. Please try again";
                    this.notifyError.Show();
                    return;
                }
            }
            else
            {
                Zones z = new Zones();

                z.Code = this.txtZoneCode.Text;
                z.Name = this.txtZoneName.Text;
                z.BookGoal = Utils.GetInt(this.txtZoneBookGoal.Text);

                z.Save();

                if (z.ZoneID > 0)
                {
                    Response.Redirect("ManageZones.aspx?v=1", true);
                }
                else
                {
                    this.notifyError.Title = "Zone creation was not successfull!";
                    this.notifyError.Text = "There was an error saving your zone. Please try again";
                    this.notifyError.Show();
                    return;
                }
            }
        }
        else
        {
            this.notifyError.Title = "Please fill in the following to continue:";
            this.notifyError.Text = error;
            this.notifyError.Show();
            return;
        }
    }

Robert Zeno

Robert Zeno
  • 67
  • 1
  • 14

2 Answers2

1

I changed OnClientClicked to OnClientClicking because there was a javascript error causing the page to break and not pull the correct data

Robert Zeno
  • 67
  • 1
  • 14
0

You have four buttons - two buttons have same name Save.

Look like you are pressing the wrong. It makes me confuse too.

protected void btnSave_Click(object sender, EventArgs e)
{
   // Not this event
}

protected void btnCreate_Click(object sender, EventArgs e)
{
   // You need to use this event.
}

For better design practice

  1. Use CSS instead of inline styles.
  2. You need to name buttons properly. For example, SaveRadButton, CreateRadButton

DO NOT use Hungarian notation such as btnXXX especially when you use third party controls like telerik.

Win
  • 61,100
  • 13
  • 102
  • 181
  • I am not pressing the wrong button. I have two click events. I am following along with breakpoints and watching it hit their respected button click events. – Robert Zeno Feb 04 '15 at 23:39
  • I have no clue what **Zones** class is. You need to debug through **Zones** class. – Win Feb 04 '15 at 23:49
  • why would I have to debug Zones? The textbox is not updating. I removed all the Hungarian notation and named the buttons properly and still no updating – Robert Zeno Feb 06 '15 at 19:43
  • `The textbox is not updating` What do you mean by not updating? Do you mean **txtZoneCode.Text** and **txtZoneName.Text** are blank inside **btnSave_Click** event when you debug through line-by-line? – Win Feb 06 '15 at 19:50
  • No they are with the original set values from the page load. Not anything I type in – Robert Zeno Feb 06 '15 at 19:52
  • Could you set a break point inside **Page_Load** event, and check code inside **if(!Page.IsPostBack)** run again after postback? – Win Feb 06 '15 at 20:08
  • Works fine. The code is true when Page.IsPostBack is hit after save button is clicked – Robert Zeno Feb 06 '15 at 20:35
  • I changed OnClientClicked to on OnClientClicking and wrote a confirm that way and it worked. There was a break in the javascript for OnClientClicked. Thank you for your help though – Robert Zeno Feb 06 '15 at 20:39
  • 1
    Not a problem. I'm glad that you figure out. – Win Feb 06 '15 at 21:00