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';"