Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Issue : When the user is writing some text in the textarea(UserControl2) and in between when he tries to click anyone of the Radiobutton(from UserControl1) the page is posted back and the text value of the textarea is lost. I have also enable viewstate for the textarea.

Please guide me. How can I save the value of the textarea when the user clicks a radio button.


C#
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PictureUploader.ascx.cs"
    Inherits="Controls_PictureUploader" EnableViewState="true" %>


    <div style="overflow:auto; height:900px;width:1000px;">
<table width="100%">
    <tr>
        <td>
            <asp:DataList ID="DataList1" runat="server" CellPadding="10" Width="1000px" RepeatColumns="7" RepeatDirection="Horizontal" BorderColor="black"
                BorderStyle="Solid" BorderWidth="1px" BackColor="LightSkyBlue" OnItemCommand="DataList1_ItemCommand">
                <ItemTemplate>
                    <table width="100%" border="0">
                        <tr>
                            <td>
                                <img id="Image1" title="Thumbnail"  runat="server" alt="No Image" style="width:100px;height:90px"
                                src='<%# DataBinder.Eval(Container.DataItem, "imagepath") %>' />
                            </td>
                        </tr>
                        <tr>
                            <td>
                               <asp:Label ID="lblImgName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "imageid") %>' ForeColor="white"></asp:Label>
                               <asp:LinkButton ID="LinkButton1" runat="server" CommandName ="Delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "imageid") %>'>Delete</asp:LinkButton>
                               
                            </td>
                        </tr>
                    </table>
                    
                </ItemTemplate>
            </asp:DataList>
            
            <asp:RadioButtonList runat="server" RepeatColumns="7" Width="100%" ID="GalleryCheck" DataTextField="imageid" DataTextFormatString="Gallery-{0}" DataValueField="imageid"
                 RepeatDirection="Horizontal" OnSelectedIndexChanged="GalleryCheck_SelectedIndexChanged" EnableViewState="true" AutoPostBack="true" OnDataBinding="GalleryCheck_DataBinding">
                 </asp:RadioButtonList>
        </td>
    </tr>
   
    <tr>
        <td>
           <asp:Button ID="Button1" Text="Done" runat="server" OnClick="btnUpload_Click"/>
            <div style = "padding:20px">
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </div>
        </td>
    </tr>
</table>
</div>

<script type = "text/javascript">


$(document).ready(function () {
        $("#<%=FileUpload1.ClientID%>").uploadify({
        'swf': '../uploadie/uploadify.swf',
        'cancelImg': '../uploadify/cancel.png',
        'buttonText': 'Browse Files',
        'uploader': '../Upload.ashx',
        'folder': 'UploadImages',
        'fileTypeDesc': 'Images',
        'fileTypeExts': '*.jpg;*.jpeg;*.gif;*.png',
        'multi': true,
        'auto': true,
        'simUploadLimit': 10,
        'queueSizeLimit': 1024

    });
   }
);
</script>



C#
protected void GalleryCheck_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sqlcommand = "update auctionimages set GalleryPic = 1 where imageid =" + GalleryCheck.SelectedValue + "; update auctionimages set GalleryPic = 0 where imageid <>" + GalleryCheck.SelectedValue + " and auctionid = " + Session["localauctionid"].ToString();
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sqlcommand);
            db.ExecuteNonQuery(cmd);
            LoadImages();
        }

        protected void GalleryCheck_DataBinding(object sender, EventArgs e)
        {
            RadioButtonList objRdList = (RadioButtonList)sender;
            DataSet objDs = (DataSet)objRdList.DataSource;

            if (objDs.Tables[0].Rows.Count > 0)
            {
                    DataRow[] oDrArray = objDs.Tables[0].Select("GalleryPic = 1");
                if (oDrArray.Length > 0)
                {
                    objRdList.SelectedValue = oDrArray[0]["imageid"].ToString();
                }
            }
        }



C#
<%@ Control Language="VB" AutoEventWireup="true" CodeFile="AddAuctionStep3b.ascx.vb" Inherits="Controls_AddAuctionStep3b" EnableViewState="true" %>
<%@ Register Src="../Controls/PictureUploader.ascx" TagName="PictureUploader" TagPrefix="uc1" %>
<script language="JavaScript" type="text/javascript" src="../wysiwyg.js"></script>

 <link href="../uploadie/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../uploadie/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="../uploadie/jquery.uploadify.js"></script>
<script type="text/javascript" src="../uploadie/swfobject.js"></script>

<script language="javascript" type="text/javascript">

function fnShowPreview(vInvId)
{
   // testwindow=window.open("../pages/PreviewHtml.aspx?listid=" + vInvId, "mywindow", "location=1,status=1,scrollbars=1,width=600,height=500,resizable=yes,menubar=no,directories=no;", false);
    //testwindow.moveTo(0,0);
   
}
function Confirmation()
{
    if(confirm('Template has changed, Do you want to reload?'))
    {return true;}
    else
    {return false;}
}

</script>
<div id="content">
		<!--  ADD PICTURES  -->

        <fieldset>
            <table>
                <tr>
                    <td>
                        Customer:
                    </td>
                    <td>
                        <asp:Label runat="server" ID="LblCustomer"></asp:Label>   
                    </td>
                    <td>
                        Location ID:
                    </td>
                    <td>
                        <asp:Label runat="server" ID="LblLocationId"></asp:Label>   
                    </td>
                    <td>
                        Inv. Id:
                    </td>
                    <td>
                        <asp:Label runat="server" ID="LblListingId"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        Listing Title:
                    </td>
                    <td>
                        <asp:Label runat="server" ID="LblInvTitle"></asp:Label>
                    </td>
                    <td>
                        Status:
                    </td>
                    <td align="left">
                        <asp:Label runat="server" ID="LblStatus"></asp:Label>   
                    </td>
                    <td>
                        Ebay Item#:
                    </td>
                    <td>
                        <asp:Label runat="server" ID="LblItemNo"></asp:Label>   
                    </td>
                </tr>
            
            </table>
        
        </fieldset>
        <fieldset>
    <legend>Listing Description and Images</legend>
        <br />
        </fieldset>
            <legend>Thumbnail Images</legend>
            <table width="100%" border="0" >
                <tr>
                    <td colspan="2">
                        <uc1:PictureUploader id="PictureUploader1"  runat="server" EnableViewState="true">
                        </uc1:PictureUploader>
                    </td>
                </tr>
            </table>
        
                 
        <fieldset>
            <legend>Listing Description</legend>
            <table width="100%" border="0">
                <tr>
                    <td>
                    <asp:Label ID="Label1" Text="Load" runat="server"></asp:Label>      
                     <asp:DropDownList ID="ddTemplates" runat="server" DataSourceID="SqlDataSource1" DataTextField="TemplateName"
                        DataValueField="TemplateId">
                    </asp:DropDownList>
                    <asp:ImageButton ID="ibtnLdTemp" runat="server" ImageUrl="~/Images/LoadBtn.png"
                                CssClass="menuButton" ImageAlign="Top" OnClick="ibtnLdTemp_Click" />
                     <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                        ConnectionString="<%$ ConnectionStrings:TMTConnectionString %>"
                        SelectCommand="SELECT [TemplateId], [TemplateName] FROM [TMT_Templates] WHERE (([ChannelId] = @ChannelId) AND ([UserName] = @sessUsrId) AND ([TemplateStatusId] in (2,3))) ORDER BY [TemplateStatusId] DESC">
                        <SelectParameters>
                        <asp:SessionParameter Name="sessUsrId" SessionField ="ParentUserId" />
                        <asp:SessionParameter Name="ChannelId" SessionField ="ChannelId" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                         
                            <asp:Label ID="Label2" runat="server" Text="Global"></asp:Label>
                            <asp:DropDownList ID="ddlGlobalTemplate" runat="server" DataSourceID="SqlDataSource2"
                                DataTextField="GlobalTemplateName" DataValueField="GlobalTemplateId">
                            </asp:DropDownList>
                            <asp:ImageButton ID="ibtnLoadTemplate" runat="server" ImageUrl="~/Images/LoadBtn.png"
                                CssClass="menuButton" ImageAlign="Top" OnClick="ibtnLoadTemplate_Click" />
                            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TMTConnectionString %>"
                                SelectCommand="SELECT [GlobalTemplateId], [GlobalTemplateName] FROM [TMT_Globaltemplates]">
                            </asp:SqlDataSource>
                                        
                                        
                            <%--<asp:Button runat="server" Text ="  Reload  " id="btnReload" />  --%>
                            <asp:Button ID="btnReload" runat="server" Text=" Reload " OnClientClick="return Confirmation();" OnClick="btnReload_Click" />
                    </td>
                    <td> </td>
                </tr>
                <%--<tr>
                 <td valign="bottom">
                    <br />
                   <asp:DropDownList runat="server" AutoPostBack="true"  ID="ddTemplateType" DataTextField="TemplateName"
                        DataValueField="TemplateId">
                        </asp:DropDownList>
                    <br />
                </td>
                </tr>--%>
                <tr>
                    <td colspan="2">
                       <%-- <asp:RadioButtonList runat="server" AutoPostBack="true" RepeatDirection="Horizontal" ID="RdTemplateType">
                            <asp:ListItem Text="Use Default Template" Selected="true" Value="Default"></asp:ListItem>
                            <asp:ListItem Text="Use Saved Template" Value="CustomizeDefault"></asp:ListItem>
                            <asp:ListItem Text="No Template" Value="NoTemplate"></asp:ListItem>
                        </asp:RadioButtonList>--%>
                    </td>
                </tr>
                
                 <tr>
                <td colspan="2">
                    <table width="100%">
                        <tr>
                            <td>
                            <asp:HiddenField id="hdnListingDesc" runat="server" Value="" />
                                <textarea id="richbox" cols="25" rows="5"  runat="server" enableviewstate="true"></textarea>
                 

                                <script type="text/javascript">
                                    generate_wysiwyg('ctl00_ContentPlaceHolder1_AddAuctionStep3b_1_richbox');
                                </script>

                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
                
                    
            </table>
          </fieldset>

		
	<fieldset>
		<asp:button id="btnSave" runat="server" Text=" Save  " Width="130px"></asp:button>
		<asp:button id="btnContinue" runat="server" Text="Edit Listing" Width="200px"></asp:button>
		<input type="button" id="btnPreview"  value="Preview Template"    önclick="openPopUp();"/>
	</fieldset>
</div>

<script language="javascript" type="text/javascript">
function openPopUp()
{
    var strValue = document.getElementById("ctl00_ContentPlaceHolder1_AddAuctionStep3b_1_richbox").value;
    window.open().document.writeln(strValue)
    return true;
}

function setListDesc()
{
    viewSource("ctl00_ContentPlaceHolder1_AddAuctionStep3b_1_richbox"); //document.getElementById("txtListingDesc").value;
    var strValue = document.getElementById("ctl00_ContentPlaceHolder1_AddAuctionStep3b_1_richbox").value;
    document.getElementById("ctl00_ContentPlaceHolder1_AddAuctionStep3b_1_richbox").value = strValue;
}

function window_onunload()
{
}
</script>



VB
Private Sub SaveDateToDatabase()

        'If Not RdTemplateType.Items(2).Selected Then
        m_Item = CType(Session(WebConstants.Session_Item), ItemType)
        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim sqlCommand As String = ""

        sqlCommand = "SaveAuctionStep3b"

        Dim cmd As DbCommand = db.GetStoredProcCommand(sqlCommand)

        If (Session("localauctionid") <> Nothing And Session("localauctionid").ToString() <> "") Then
            db.AddInParameter(cmd, "@auctionid", DbType.Int32, Session("localauctionid").ToString())
        Else
            db.AddInParameter(cmd, "@auctionid", DbType.Int32)
        End If

        

        Dim strTxt As String
        strTxt = richbox.InnerHtml.Replace(Request.Url.AbsoluteUri.ToString().Replace("&", "&").Replace("&", "&"), "")

        Dim strTxt2 As String
        strTxt2 = richbox.InnerHtml.Replace(Request.Url.AbsoluteUri.ToString().Replace("&", "&").Replace("&", "&"), "")

        db.AddInParameter(cmd, "@desc", DbType.String, Replace(strTxt2, "'", "''"))

        If (ddTemplates.Items.Count <> 0) Then
            db.AddInParameter(cmd, "@TemplateType", DbType.String, Replace(ddTemplates.SelectedItem.Value, "'", "''"))
        Else
            db.AddInParameter(cmd, "@TemplateType", DbType.String, "NoTemplate")
        End If


        db.ExecuteNonQuery(cmd)
        'End If

    End Sub



VB
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      If (checkSession()) Then

          If (String.IsNullOrEmpty(Session("ChannelId"))) Then
              Session("ChannelId") = "1"
          End If
          If Not IsPostBack Then
              If (Request.QueryString("listid") <> "") Then
                  GetDataFromDatabase()
                  LoadTemplates()
                  'getTemplateDetails()
              End If
          End If
      Else
          Response.Redirect("~/Pages/Login.aspx", False)
          Return
      End If

  End Sub
Posted

1 solution

Text Area is getting clear because the page is getting posted back on click of Radio Button. To over come the issue try saving the Text Area value in ViewState and re-populate it again from the view state
 
Share this answer
 
Comments
Abhishek Luv 23-Sep-13 6:43am    
Thanks for your reply. How can I do that. Sample code.I have changed the html textarea to asp.net textbox control
Abhishek Luv 23-Sep-13 9:13am    
I am trying to get the text of the textbox in the Viewstate and then providing it as a parameter to my SP.


ViewState("rich") = richbox.Text.Replace(Request.Url.AbsoluteUri.ToString().Replace("&", "&").Replace("&", "&"), "")

db.AddInParameter(cmd, "@desc", DbType.String, Replace(ViewState("rich"), "'", "''"))
Abhishek Luv 25-Sep-13 2:23am    
The textbox value gets posted properly on a radiobutton postback when I don't use this.


<%--<script type="text/javascript">
generate_wysiwyg('ctl00_ContentPlaceHolder1_AddAuctionStep3b_1_richbox');
</script>--%>

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