Wednesday, November 17, 2010

Detect Browser Capabilities in ASP.NET

    protected void Button1_Click(object sender, System.EventArgs e)
    {
        System.Web.HttpBrowserCapabilities browser = Request.Browser;
        string s = "Browser Capabilities\n"
            + "Type = " + browser.Type + "\n"
            + "Name = " + browser.Browser + "\n"
            + "Version = " + browser.Version + "\n"
            + "Major Version = " + browser.MajorVersion + "\n"
            + "Minor Version = " + browser.MinorVersion + "\n"
            + "Platform = " + browser.Platform + "\n"
            + "Is Beta = " + browser.Beta + "\n"
            + "Is Crawler = " + browser.Crawler + "\n"
            + "Is AOL = " + browser.AOL + "\n"
            + "Is Win16 = " + browser.Win16 + "\n"
            + "Is Win32 = " + browser.Win32 + "\n"
            + "Supports Frames = " + browser.Frames + "\n"
            + "Supports Tables = " + browser.Tables + "\n"
            + "Supports Cookies = " + browser.Cookies + "\n"
            + "Supports VBScript = " + browser.VBScript + "\n"
            + "Supports JavaScript = " +
                browser.EcmaScriptVersion.ToString() + "\n"
            + "Supports Java Applets = " + browser.JavaApplets + "\n"
            + "Supports ActiveX Controls = " + browser.ActiveXControls
                  + "\n"
            + "Supports JavaScript Version = " +
                browser["JavaScriptVersion"] + "\n";

        TextBox1.Text = s;
    }

Creating control at runtime in C#Codebehind

 private DataSet GetTemplatesDs()
        {
            con.Open();
            string sqlToExecute = "select TemplateName,Subject  from Template";
            SqlDataAdapter da = new SqlDataAdapter(sqlToExecute, con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Distribution_Mail_Template");
            con.Close();
            Session["TemplatesDs"] = ds;
            return ds;
        }

private void PopulateUI()//user defined function
{
                  Label[] additionalApplicants;
                  DataSet gettemplatesDs = GetTemplatesDs()
                        if (gettemplatesDs .Tables.Count > 0 && gettemplatesDs .Tables.Count > 0)
                        {
                            for (int i = 0; i < gettemplatesDs .Tables[0].Rows.Count; i++)
                            {
                                additionalApplicants = new Label[gettemplatesDs .Tables[0].Rows.Count];
                                additionalApplicants[i] = new Label();
                                additionalApplicants[i].ID = gettemplatesDs .Tables[0].Rows[0][""].ToString();
                                additionalApplicants[i].Text =gettemplatesDs .Tables[0].Rows[0][""].ToString();
                               this.Page.Controls.Add(additionalApplicants); //Adding controls to webpage
                            }
                        }
     }

Retain previously selected values by using Dictionary

 Class1.cs:
=======
 public void SaveSelectedOptions(Dictionary<string, string> selectedoptions)
        {
            Dictionary<string, string> tempDict = (Dictionary<string, string>)System.Web.HttpContext.Current.Session["SelectedValues"];
            if (tempDict != null)
            {
                foreach (var pair in selectedoptions)
                {
                    if (tempDict.ContainsKey(pair.Key)) // True
                    {
                        tempDict.Remove(pair.Key);
                    }

                    tempDict.Add(pair.Key, pair.Value);
                }
               System.Web.HttpContext.Current.Session["SelectedValues"] = tempDict;
            }
            else
            {
                Dictionary<string, string> newdict = new Dictionary<string, string>();
                foreach (var pair in selectedoptions)
                {
                    newdict.Add(pair.Key, pair.Value);
                }
                System.Web.HttpContext.Current.Session["SelectedValues"] = newdict;
            }
        }

Defaulst.aspx.cs
=============
  private void saveSelectedOptions()
        {
            Dictionary<string, string> selectedoptionsDict = new Dictionary<string, string>();
            selectedoptionsDict.Add(txtCompany.ID, txtCompany.Text);
            selectedoptionsDict.Add(txtID.ID, txtID.Text);
            selectedoptionsDict.Add(txtLastName.ID, txtLastName.Text);
            selectedoptionsDict.Add(txtShowDays.ID, txtShowDays.Text);
            selectedoptionsDict.Add(ddlAStatus.ID, ddlAStatus.SelectedIndex.ToString());
            selectedoptionsDict.Add(ddlChapterStatus.ID, ddlChapterStatus.SelectedIndex.ToString());
            selectedoptionsDict.Add(ddlDU.ID, ddlDU.SelectedIndex.ToString());
            selectedoptionsDict.Add(ddlPaid.ID, ddlPaid.SelectedIndex.ToString());
            selectedoptionsDict.Add(ddlRSatus.ID, ddlRSatus.SelectedIndex.ToString());
            Class1.SaveSelectedOptions(selectedoptionsDict);
        }

private void PopulatedPreSelectedOptions()
    {
            Dictionary<string, string> tempDict = (Dictionary<string, string>)Session["SelectedValues"];
            if (tempDict != null)
            {
                foreach (var pair in tempDict)
                {
                    string controlname = pair.Key;
                    Control control = this.FindControl(controlname);
                    if (control is DropDownList)
                    {
                        DropDownList drl = (DropDownList)this.FindControl(controlname);
                        drl.SelectedIndex = Convert.ToInt32(pair.Value);
                    }
                    else if (control is TextBox)
                    {
                        TextBox txt = (TextBox)this.FindControl(controlname);
                        txt.Text = pair.Value;
                    }
                }
                grvFiledsResult.DataSource = Session["SearchResultDS"];
                grvFiledsResult.DataBind();
            }

        }

Monday, November 15, 2010

Creating Table dynamically in codebehind C#

.CS Code:
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("MemberType", typeof(string));
table.Rows.Add(membername, membertype);
table.Rows.Add(memberType);
DataSet myds = new DataSet();
myds.Tables.Add(table);
 test.DataSource = myds;
 test.DataBind();
.ASPX Code:
<table >
                    <tr><td>
                    <asp:GridView ID="test" AutoGenerateColumns="false" runat="server" ShowHeader="false" GridLines="None">
                    <Columns>
                  
                   asp:BoundField DataField="Name" />
                    <asp:BoundField DataField="MemberType" />
                    </Columns>
                   
                    </asp:GridView></td></tr></table>