Monday, 30 April 2012

Read And Write Cookies Values

Working With Cookies On Different Domains

Cookies provide a means in Web applications to store user-specific information. For example, when a user visits your site, you can use cookies to store user preferences or other information. When the user visits your Web site another time, the application can retrieve the information it stored earlier.

1)Working With Cookie Start From Initialization,Lets See How This Could Be Done

             
                HttpCookie cookie = Request.Cookies["Preferences"];
                if (cookie == null)
                {
                    cookie = new HttpCookie("Preferences");
                }
                cookie["imgforlogo"] = "~/AnyImg.png";//Filling some value in
cookie
                Response.Cookies["imgforlogo"].Expires = DateTime.Now.AddDays(-1);//Set expire time of cookie to yesterday
               //cookie.Expires = DateTime.Now.AddYears(1);                                         //Or Setting expire time of cookie to 1 year
                Response.Cookies.Add(cookie);
                string a = cookie["imgforlogo"];
                Response.Cookies["imgforlogo"].Domain = "http://www.BloggerExample.com/blogger";//Providing this cookie                                                                                                                                           value to any other subdomain  you  have                                                                                                                                              have.else you can not access cookies in                                                                                                                                          diffrent domains

                Response.Cookies["imgforlogo"].Domain = "http://localhost:60128/
blogger/";//if you want to check it  on                                                                                                                      diffrent projects on localhost



             
2)You Can Check Cookie Value On Other Page (In the same project or project on other Domain)By Providing Below Code On Page_Load or Wherever you Require
           
           HttpCookie cookie = Request.Cookies["Preferences"];           
            if (cookie == null)
            {
                      Response.Redirect("~/ErrorPage.aspx");//Redirect if there is no cookie found
            }
            else
            {
                Image1.ImageUrl = cookie["imgforlogo"];// now filling 
ImageUrl of Image1 Control with saved cookie
             }



       





Monday, 23 April 2012

Find Control In Grid View

Find Control In Grid View

Here We Will Find Control In Grid View From Two Well Know Events RowCommand,DataBound And Delete,Update.



1)In Normal Grid View DataBound


 if (e.Row.RowType == DataControlRowType.DataRow)
        {
                     string NormalValue=     ((Label)e.Row.FindControl(" Label 2")).Text ;
        }


   



2)In Normal GridView RowCommand


for (int i = 0; i <  GridView1.Rows.Count; i++)
            {
           
         ((Label)GridView1.Rows[i].FindControl("Label1")).Text);
         ((Image) GridView1 .Rows[i].FindControl("Image1")).ImageUrl);

             }





3)In Normal GridView Delete,Update Command

((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text);


If You Have Any Data Key Then...


GridView1.DataKeys(e.Row.RowIndex).Value.ToString()





4)When its Nested  GridView  than in row command you can find any (lable,linkbutton,button,textbox)value by putting this..


            LinkButton btn = e.CommandSource as LinkButton;//the button in which this event fired
            GridViewRow row= btn.NamingContainer as GridViewRow;
            Label POST = row.FindControl("Label1") as Label;
            Label COMMENTID = row.FindControl("Label2") as Label;
            LinkButton Likelinkbutton = row.FindControl(" LinkButton1") as LinkButton;


or direct

string valuefromdatabase= e.CommandArgument.ToString();

if you have binded control with CommandArgument!



5)Nested Grid View DataBound


 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridViewRow row = e.Row;
           string commentingid= ((Label)row.FindControl( Label 2")).Text;
         }


















Wednesday, 18 April 2012

Simple Css Style For AJAX AutoComplete Extender

AJAX: Simple CSS For AutoComplete Extender

 I know your AutoComplete Extender is working fine,but the thing is you are not happy with its presentation when its showing generating list in 20px padding difference with a bullet.


 1)if you have CSS like this 













2)Then apply below CSS to make it more simple like











 <style type="text/css">
   /*AutoComplete flyout */
.completionList {
border:solid 1px #444444;
margin:0px;
padding:2px;
height: 100px;
overflow:auto;
background-color: #FFFFFF;
}

.listItem {
color: #1C1C1C;
}

.itemHighlighted {
background-color: #ffc0c0;
}
   </style>

3)and further you know you must edit these thing with your extender too.
  
     CompletionListCssClass="completionList"
     CompletionListHighlightedItemCssClass="itemHighlighted"
     CompletionListItemCssClass="listItem"


If you are very new to AJAX autocomplete then here is a sample of autocomplete Extender on work.
                                
you may also like.....
Ajax Progress Bar.
Autocomplete Extender.
Ajax Calender Extender.
div class="MsoNormal" style="color: #3d85c6;">



Before I End I would like to recommend new kind of reporting which is which is currently a hot topic in IT industry, that is Power bi,and you can head start with it by viewing this popular video on power bi which can make you power bi professional in very simple stepsas below.


Tuesday, 17 April 2012

Authenticate Pragmatically:Bind URL in TreeView Dynamically And Authorize User To View /Access Fix URLs

Authorize User To View/Access Specified URL From Tree View.
Bind URL in TreeView Dynamically For Particular User.
Authorize User To Access Fix URL for him..


Its Is Strictly saying to all reader that this is Not actual ASP.net authorization/authentication.
its just way of doing work in authenticate manner.
like proving different URL to different USER,It was just what i made for one of my project.and shared with you guys.




Let say you have a TreeView Control and its showing Its nodes and their sub nodes like middle one tree view sample from below image.

Now what you want is when you login,you'll see TreeView as it is in  left side.
At the same time when i Logged in from my end i'll see the same TreeView as it is in right side preview.





For my this post about TreeView,I'm Using Head Node and Child Node hope you understand what i mean to them..







Now As the TOP most Image we looked up a kind of image where i was saying..
that we provided...
particular CHILD NODE to particular HEAD NODE to particular USER...
So Lets first Define Head Node,then We Will Define Child Node and finally provide
access to the USER that which Nodes he can access..

So here we begin....
1)Define Head Node..








2)Define CHILD NODE...





3)Provide Access To Particular Child Nodes of Particular Head Nodes To User.












 4)And We Completed!!!
just for  a sample I am showing you a image which shows which kind of output you'll get in TreeView






 5)Now to remind you its not statically define....means User,Head Node,Child Node.....
Its all stored in database Dynamically.
For that we need FOUR important tables as shown below...
don't worry I've  attached it in download sample.





 6)And To Manage the Autherization/Create User  You must save details first as in below form..






7)As I know you better know how to add,edit update and delete SQL table from grid view.so lets move ahead..and come to the main point.
what we feel is every thing is bit easy other than how TreeView Understanding which thing to show or Not Show to the particular.

Its all because of our fill_Tree() which is binding on page load.and after getting proper data its Binding appropiate Nodes and Child Nodes for particular USER.




    void fill_Tree()
    {
        con.Open();
        SqlCommand SqlCmd = new SqlCommand("SELECT DISTINCT dbo.tbl_NodeParentTable.ParentName, dbo.tb_RoleManagement.Acc_Id, dbo.tbl_NodeParentTable.ParentID FROM         dbo.tb_RoleManagement INNER JOIN                       dbo.tbl_NodeAuthorizeTo ON dbo.tb_RoleManagement.Acc_Id = dbo.tbl_NodeAuthorizeTo.UserName INNER JOIN                       dbo.tbl_NodeParentTable ON dbo.tbl_NodeAuthorizeTo.Node = dbo.tbl_NodeParentTable.ParentID where Acc_Id=" + Request.Cookies["RoleID"].Value + "", con);
        SqlDataReader Sdr = SqlCmd.ExecuteReader();
        SqlCmd.Dispose();
        string[,] ParentNode = new string[100, 2];
        int count = 0;
        #region "Collect Parent Heading"
        while (Sdr.Read())
        {
            ParentNode[count, 0] = Sdr.GetValue(Sdr.GetOrdinal("ParentID")).ToString();
            ParentNode[count++, 1] = Sdr.GetValue(Sdr.GetOrdinal("ParentName")).ToString();
        }
        #endregion
        Sdr.Close();
        //below loop will execute per Heading
        for (int loop = 0; loop < count; loop++)
        {
            TreeNode root = new TreeNode();
            root.Text = ParentNode[loop, 1];
            root.Target = "";
            root.NavigateUrl = "";
            SqlCommand Module_SqlCmd = new SqlCommand("SELECT     dbo.tb_RoleManagement.UserName, dbo.tb_RoleManagement.Acc_Id, dbo.tb_RoleManagement.Password, dbo.tbl_NodeAuthorizeTo.Node,                        dbo.tbl_NodeAuthorizeTo.ChildNode, dbo.tbl_NodeChildTable.ChildName, dbo.tbl_NodeChildTable.URL FROM         dbo.tb_RoleManagement INNER JOIN                       dbo.tbl_NodeAuthorizeTo ON dbo.tb_RoleManagement.Acc_Id = dbo.tbl_NodeAuthorizeTo.UserName INNER JOIN                       dbo.tbl_NodeChildTable ON dbo.tbl_NodeAuthorizeTo.ChildNode = dbo.tbl_NodeChildTable.Snno where Acc_Id=" + Request.Cookies["RoleID"].Value + "  and dbo.tbl_NodeAuthorizeTo.Node =" + ParentNode[loop, 0], con);
            SqlDataReader Module_Sdr = Module_SqlCmd.ExecuteReader();
            //below loop will execute Sub Nodes of Single heading Coming From above for loop
            while (Module_Sdr.Read())
            {
              
                TreeNode child = new TreeNode();
                child.Text = Module_Sdr.GetValue(Module_Sdr.GetOrdinal("ChildName")).ToString();
                child.Target = "";
                child.NavigateUrl = Module_Sdr.GetValue(Module_Sdr.GetOrdinal("URL")).ToString();
                root.ChildNodes.Add(child);

            }
            Module_Sdr.Close();

            // Add root node to TreeView
            TreeView1.Nodes.Add(root);

        }

        /*
        * By Default, when you populate TreeView Control programmatically, it expends all nodes.
        */
        TreeView1.CollapseAll();
        con.Close();

    }



8)And Finally We Called Our fill_Tree() Function.


    protected void Page_Load(object sender, EventArgs e)
    {        
        fill_Tree();
       
    }

9)In Between all these few times you may also required that you provided a fix time for user to access the page,other wise redirect him to timeout.html page!

So just look up at my page (AcessOnTime.cs page under Authentication Folder )you'll understand what i did with storing User Id in cookies.
So After you login just open AcessOnTime.aspx and you'll see the currect page (with msg time is on) if you logged in time.but if you logged in a time which is out of define time for you  you'll simply redirected to timeout.html page.

And finally here is what you're lookin for...

Monday, 2 April 2012

Total in Gridview Footer

Show Total In Grid View Footer


Some time you require to show total in footer of a Gridview which will generate sum of a column automatically.



With a brief  explanation...
let take a table Table_1

 CREATE TABLE [dbo].[Table_1]
(
    [Sno] [int] IDENTITY(1,1) NOT NULL,
    [Product] [varchar](50) NULL,
    [Cost] [float] NULL
)



which will give you this kind of GridView when Binding with it with some values..



Now you require to sum up cost in footer ,for that we'll take a Label On Footer of Cost ItemTemplate
as below ...

Below code is the only factor to calculate some ...

     double totqty = 0;
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
  
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label totpcs = (Label)e.Row.FindControl("LabelC");
            totqty = totqty + (Convert.ToInt32(totpcs.Text));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            ((Label)e.Row.FindControl("LabelFooter")).Text = totqty.ToString();
            Label1.Text = totqty.ToString();//to make it visible outside grid

         }




here LabelC is lable on normaly showing in itetemplate
LabelFooter is lable on footer....


And Finally When You Execute The Programe Output Will Be Like That....
And here is finally what you're looking for

Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...