Tuesday, December 16, 2008

How to Get Printer option When a Button is Clicked?

The First thing to note here is when you click on the Button,the page content should be printed but the Button say PrintButton should not appear in the document that is going to get printed.


When you simply call window.print() , this problem occurs.

How to Solve this?

The Source Code for the Button created is as shown below:

<asp:Button ID="btnPrint" runat="server" CssClass="btn1" Text="Print Me"
OnClientClick="javascript:fun(this,'print')"/>

Observe that I am calling a function fun with 2 arguments ,i.e this and print.
'this' refers to Button with ID='btnPrint' i.e the Button which we want to hide after clicking.

The Second argument is nothing but Css ClassName.

The CssClass Code is as Below:

<style type="text/css">
.print
{
visibility:hidden;
}
.btn1
{
color:#050;
font: bold 84% 'trebuchet ms',helvetica,sans-serif;
background-color:#fed;
border:1px solid;
border-color: #696 #363 #363 #696;
}
</style>

The JavaScript function fun will be as shown below:

<script type="text/javascript" language="javascript">
function fun(loc,p)
{
loc.className=p
window.print();
}

</script>

Confirmation for Delete Button in GridView

I am displaying student table in GridView. 'sno' is the primarykey for the student table.
That is why Iam binding that value to CommandArgument of the Button.This can be used in code part
as 'e.CommandArgument' which is also explained later.

The Source Code in GridView is as shown below:

<Columns>

<asp:BoundField HeaderText="SNO" DataField="sno" />
<asp:BoundField HeaderText="NAME" DataField="sname" />
<asp:BoundField HeaderText="CITY" DataField="city" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" CommandArgument='&lt%#Eval("sno") %&gt' OnClientClick="javascript:return fun();"/&gt
</ItemTemplate>
</asp:TemplateField>

</Columns>

Write JavaScript code below in Head Part:

&ltscript type="text/javascript" language="javascript">
function fun()
{
if (confirm("Do u want to really Delete?")==true)
return true
else
return false
}
</script>

In the GridView RowCommand Event , you will write the code for deleting the record.
The code will be as shown below:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
string squery = "delete blog_student where sno=" + e.CommandArgument.ToString();
SqlCommand cmd = new SqlCommand(squery, con);
con.Open();
cmd.ExecuteNonQuery();
GetStudentData();
}
}

Friday, December 12, 2008

Open a new window When a GridView s Control is Clicked

A GridView is there with multiple rows binded to it from database.You want a functionality to navigate to a new page when user clicks on a Coressponding Control placed on each GridView Row.
How to achieve this?
Use a HyperLink Field as shown below:

<asp:HyperLink ID="HyperLink4" runat="server" ToolTip='Click To Edit' ImageUrl="pen.png" NavigateUrl="<%# String.Format("javascript:void(window.open('../Dec1/EditNewTemp.aspx?tempid={0}'),null,'width=525,height=450,top=325,left=2000,scrollbars=yes,resizeable=no');", Eval("tempid")) %>" > </asp:HyperLink>


Aliter:
Write the code as below in RowDataBound event of GridView to pass required value to a
Java Script Function.





protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == -1) return;
Button B = (Button)e.Row.FindControl("btn1");
B.Attributes.Add("onclick", "JavaScript:fun1('" + DataBinder.Eval(e.Row.DataItem, "pinno") + "')");


}

Note: btn1 is the ID of Button control in GridView. When we click on this Button , the corresponding rows PrimaryKey Column value should be passed to a JavaScript function.

The javacript function is as below:

function fun1(id)
{
window.open("Default2.aspx?pinno="+id,"New","width=400,height=200,left=0,top=100");
}

Our requirement is to watch results in a different page which requires a QueryString
value to execute its query.

When user clicks on 'Show' Button corresponding to PinNo=3 ,the Result will be as shown below :





Aliter 2 : Opening a New Window using Response.Redirect:

<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:ImageButton CommandName="View" ID="View" ToolTip='Click To View' runat="server" CommandArgument='<%# Eval("tempid") %>' ImageUrl="view.gif" Width="20" Height="20" OnClientClick="form1.target ='_blank';"/>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>


Note : form1 is the ID of the Form.You need to change this to yours Form ID in order to observe the
change in functionality.

The code will be written in RowCommand Event of GridView as shown below:

protected void VideoListGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{


if (e.CommandName == "View")
{
string tid = e.CommandArgument.ToString();
Session["tid"] = tid.ToString();

Response.Redirect("../Dec8/NewTempAPI.aspx?tempid="+tid);


}
}

Suppose that your GridView contains other buttons which need to be opened on the same window.
Then What should we do?

Simply include this code part in the source code for Button.

OnClientClick="form1.target ='_self';"

Thursday, November 27, 2008

How to make 'userid' Unique?

Hello Friends!

Today I want to share with you most common functionality while you develop any registration page.
You dont want to make userid as primary key but you still want to make it unique.How?
That is Simple.You need to first access all the data in Register table in to a DataSet (say Ds).

Let us consider txtUserName as ID of TextBox in which user enters his 'userid'.

The keypoint here is the user who is registering may be the first user of you.Then Your database contains nothing.You also need to check this condition.

A Label with ID as 'lblCheck' displays the availability of userid.

The Code part will be as shown below :

if (ds.Tables["user"].Rows.Count > 0)
{
string uname = txtUserName.Text;

foreach (DataRow R in ds.Tables["user"].Rows)
{
if (uname.CompareTo(R["userid"].ToString()) == 0)
{
lblCheck.Visible = true;
lblCheck.Text = "Unavailable ,Please Choose another name.";
break;
}
else
{
lblCheck.Visible = true;
lblCheck.Text = "Username available.";
}
}
}
else
{
lblCheck.Visible = true;
lblCheck.Text = "Username available.";
}


Aliter :


There is another way to do this.Using command object and SqlDataReader we can simply write code as
below:

cmd = new SqlCommand("select * from Register where userid='"+txtUserName.Text+"'",con);
con.Open();
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
lblCheck.Visible = true;
lblCheck.Text = "Unavailable ,Please Choose another name.";
}
else
{
lblCheck.Visible = true;
lblCheck.Text = "Username available.";

}

Wednesday, November 19, 2008

Display Dates with out time in GridView !









Actually GridView displays date with time only even though you store just date in database say 12/2/2003.

How to overcome this?

Use DataFormatString attribute of BoundField to format your data in your source code while you write bound fields code of GridView.

The below sample shows this:

< asp:BoundField DataField="StartDate" HeaderText="StartDate" DataFormatString="{0:d}" HtmlEncode="false" / >
The result will be shown as below:


You need to set HtmlEncode to false also to achieve only date.Otherwise date will be displayed along with time.
There is another way to achieve the same result.
you will write a query to retrieve data from database and you use SqlDataAdapter for this.

In the above shown GridView , the lines of code written to retrieve data are shown below:

private void GetData()
{
string squery="select projectid,projectname,projectdescp,startdate,enddate,
clientname from project";
da = new SqlDataAdapter(squery, con);
bldr = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "Project");
GVProject.DataSource = ds.Tables["Project"];
GVProject.DataBind();
}

Instead of above query replace squery as shown below:

string squery=select projectid,projectname,projectdescp,convert(varchar,startdate,101) as startdate,convert(varchar,enddate,101) as enddate,clientname from project.

Thursday, November 13, 2008

Why Ajax CalendarExtender is not selecting all Dates in Calendar?

See Guys,the problem may be because you have forgotten to set PopupPosition of CalendarExtender.Better specify the PopupPosition to "BottomLeft" and try.
If still the problem exists it may be because of the table you have taken.You might have placed the TextBox Control and CalenderExtender in a table.
Most often we are habituated to drop the table from Layout --> Insert Table and then we willposition it and resize it.This cause us trouble unknowingly.

What happens?
When we manually position the table, the z-index of the 'style' attribut of 'table' tag is set to a greater value say 100.So the best thing is to write code for table and set the position at source side appropriately.Thats the reason for CalendarExtender not displaying some of the dates in calendar.You adjust the z-index of style attribute to 2 and check.
Hurray it works!