0

Here is my mark up code:

<p>
<label>Reference Code:</label>
<span class="field">
<asp:TextBox ID="GenerateCode" runat="server" CssClass="smallinput random" ></asp:TextBox>
<asp:HiddenField ID="txthidden" runat="server" />
<br /><small style="color: red">Please write the code at the back of yours check</small>
</span>
</p>

here is my random function:

function randomString()
    {
        var chars ="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var result = '';
        for (var i = 8; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
        return result;
    }

here's how i get the random to be displayed in the textbox:

    function generateRandomNumber() {
            var rndStr = randomString();
            $("#<%=GenerateCode.ClientID%>").val(rndStr);
            $("#<%=txthidden.ClientID%>").val(rndStr);

        }

    window.onload = function () {
        generateRandomNumber();
    }

here's how i save it in the database:

 string EchequeMaster = ClassEmailCheque.InsertEmailCheque(SenderID, Convert.ToInt32(ddlCurrency.SelectedIndex), Convert.ToDecimal(Amount.Text), ChequeNumber.Text, GenerateCode.Text, PendingStatus, DateTime.Now);

I solved this by using hidden field...i uodated my codes for reference.

aianLee
  • 61
  • 4
  • 17

1 Answers1

0

Textboxes that are disabled (property Enabled = false) will not post back any value. The solution is to have both a disabled textbox (to show the value to the user) and a hidden field which will post back the value. You could re write your code to something like this:

<asp:TextBox ID="txtGenerateCode" runat="server" CssClass="smallinput random" />
<asp:HiddenField ID="hdnGenerateCode" runat="server" class="random" />

function generateRandomNumber() {
        var rndStr = randomString();
        $(".random").val(rndStr);
    }

Hope this helps!

mortb
  • 9,361
  • 3
  • 26
  • 44
  • See also http://stackoverflow.com/questions/7570652/how-to-keep-the-text-of-a-read-only-textbox-after-post-back – mortb Jun 18 '13 at 09:40
  • I've edited the code above. I am pretty sure that you can write just class instead of CssClass. Try my changes and tell us if it works :) – mortb Jun 19 '13 at 08:25
  • i also tried using class...but still the same error message..but i solved my problem..thanks anyway.. i used hidden field...see my code... – aianLee Jun 19 '13 at 08:29
  • Yeah, most important is that the problem is solved. Good luck! – mortb Jun 19 '13 at 09:54