Friday, 23 December 2011

How to write into New line in Java Script

        private void MyFunction()
        {
            string QuoteStatus = "Please enter values in Billing Information^Please enter values in Nature of Business^Please enter values in Policy/Liability Coverage^Please enter values in Additional Int^Please enter values in Property Details";
            string[] strError = QuoteStatus.Split('^');
            string strErr = "";

            for (int i = 0; i < strError.Length; i++)
            {
                strErr += strError[i] + "
\\n";
            }
            Response.Write("<script language='javascript'>alert('" + strErr +"');</script>");
        }


Output would be something like :

Control 'UserControl1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server

Scenario :-->>
       I have created a user Control, which have a Gridview Control(any Databound Control you can set) and one Export button
, which is used to Export Current displayed Data in Grid to Excel. After Creating user Control, i have created a new ASPX page
and registered this user control to this newly created ASPX page. and Simply Run this Project. But this give me a RUN Time
Error, saying that "Control 'UserControlID1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server".
So Get rid of this Problem, I have tried following Solution.
Solution :-->>
       
       1. User Control ASPX :
              <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControlRenderingInMainPage.ascx.cs" Inherits="testApplication.UserControl.UserControlRenderingInMainPage" %>
<asp:Panel ID="pnlUserControl" runat="server">
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
        AllowSorting="True">
    </asp:GridView>
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" Font-Italic="True" ForeColor="#FF66FF"
        onclick="Button1_Click" Text="Export to Excel" Width="100px" Visible="false"/>
    <br />
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</asp:Panel>

       2. User COntrol Code behind Page :
  using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace testApplication.UserControl
{
    public partial class UserControlRenderingInMainPage : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindToGridView();
        }
        private void BindToGridView()
        {
            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["attribiuteStr"].ConnectionString))
            {
                using (SqlDataAdapter da = new SqlDataAdapter("select * from GeneralInformation", con))
                {
                    da.Fill(ds);
                    if (ds.Tables[0]!= null && ds.Tables[0].Rows.Count > 0)
                    {
                        Button1.Visible = true;
                        GridView1.DataSource = ds.Tables[0].DefaultView;
                        GridView1.DataBind();
                    }
                }
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=>MyTestFile_" + DateTime.Now.ToShortDateString() + ".xls");
            Response.Charset = "";
            Page.EnableViewState = false;
            Response.Buffer = true;
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter ExcelHtmlWriter = new HtmlTextWriter(stringWrite);
            pnlUserControl.RenderControl(ExcelHtmlWriter);
            string style = @"<style> .text { mso-number-format:0\.00;text-align:right; } </style> ";
            Response.Write(style);
            Response.Write("<table><tr><td width = '100%' align = 'Left'><font face='verdana, arial' size='2'><b>Summary Details</b></font></td></tr></Table> ");
            Response.Write(stringWrite.ToString());
            Response.Flush();
            Response.End();
        }
    }
}
      3. My ASPX Page where i have used this newly created User COntrol :
              <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RenderUserControlPage.aspx.cs" Inherits="testApplication.ASPX.RenderUserControlPage" %>
<%@ Register Src="~/UserControl/UserControlRenderingInMainPage.ascx" TagPrefix="UC1" TagName="UserControl1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <UC1:UserControl1 ID="UserControlID1" runat="server"/>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
      4. Code behind for this ASPX Page :
             using System;
using System.Web.UI;
using System.IO;
namespace testApplication.ASPX
{
    public partial class RenderUserControlPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RenderUserControlToStringForm();
        }
        private string RenderUserControlToStringForm()
        {
            System.Web.UI.UserControl myControl = (System.Web.UI.UserControl)LoadControl("~/UserControl/UserControlRenderingInMainPage.ascx");
            using (TextWriter myTextWriter = new StringWriter())
            using (HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter))
            {
                myControl.RenderControl(myWriter);
                return myTextWriter.ToString();
            } 
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Do nothing */
        }
        public override bool EnableEventValidation
        {
            get { return false; }
            set { /* Do nothing */}
        } 
    }
}
Conclusion :-->>
        And it Works like a charm.

     Hope this helps to You.
     Correct Me, If I am Wrong!!!

Tuesday, 8 November 2011

:: How to get values of all Controls inside a Gridview using Javascript ::

function GetInitialGridValues(grdID) {
        debugger;
        var szAdd = "", szUpdate = "";
        var inputTags;
        var selectTags;
        document.forms[0].hidDefaultValues.value = "";
        document.forms[0].hidDefaultNames.value = "";
        if (grdID != null) {
            inputTags = grdID.getElementsByTagName("input");
            selectTags = grdID.getElementsByTagName("select");
        }  //Here getting all the text box and check boxes
        else {
            alert('grdID is null');
        }
        for (index = 0; index < inputTags.length; index++) {
            if (String(inputTags[index].type) == "text") {
                document.forms[0].hidDefaultNames.value = document.forms[0].hidDefaultNames.value + inputTags[index].id + "~";
                document.forms[0].hidDefaultValues.value = document.forms[0].hidDefaultValues.value + inputTags[index].value + "~";
            }
            else if (String(inputTags[index].type) == "checkbox") {
                document.forms[0].hidDefaultNames.value = document.forms[0].hidDefaultNames.value + inputTags[index].id + "~";
                document.forms[0].hidDefaultValues.value = document.forms[0].hidDefaultValues.value + inputTags[index].checked + "~";
            }
            else if (String(inputTags[index].type) == "radio") {
                document.forms[0].hidDefaultNames.value = document.forms[0].hidDefaultNames.value + inputTags[index].id + "~";
                document.forms[0].hidDefaultValues.value = document.forms[0].hidDefaultValues.value + inputTags[index].checked + "~";
            }
        }
        for (index = 0; index < selectTags.length; index++) {
            if (String(selectTags[index].type) == "select-one") {
                if (selectTags[index].length == 1) {
                    defVal = selectTags[index].options[0].text;
                }
                else {
                    for (var optCounter = 0; optCounter < selectTags[index].length; optCounter++) {
                        if (selectTags[index].options[optCounter].selected) {
                            document.forms[0].hidDefaultNames.value = document.forms[0].hidDefaultNames.value + selectTags[index].id + "~";
                            document.forms[0].hidDefaultValues.value = document.forms[0].hidDefaultValues.value + selectTags[index].options[optCounter].text + "~";
                            break;
                        }
                    }
                }
            }
        }
        return false;
    }