Category Archives: DataList

[RESOLVED] Automatic Printing in asp.net 2.0 C#

I want to print a page after page load direct to default printer , without clicking. I mean bypass the steps of pop-up box and print.

Is this possible?

 

Thanks.

 

 

No, it's not. It's a security thing ... can you imagine what a malicious website could do with this? [^o)]

i Know this is possible but I dont now how? Atletest like to do print gridview automatically.

It is not possible using ASP.NET/JavaScript.

I have no idea what "Atletest" is but ... the only way to accomplish this to use an add-in of some type (i.e. an ActiveX control, which I have seen some people do). But you cannot do it directly from a web page.

I guess, it is possible whatever method you write for pop-up box for printing, Include that method in page_load event. it may help you.

yes  ActiveX control i like to know more about this pl. help.

If I remember correctly, it simply sent keys to IE ... though it's been a long time.

Keep in mind that you'll have some deployment and trust challenges ... it's not a control that should be marked "Safe for Scripting" as it accesses local resources.

Hi,

Did  you find any resolution to your problem. I need something similar.

When any attendee comes to conference he has a confirmation number(barcode). The barcode reader reads the barcode and prints his tickets to the default printer.

Can you please help if you found the solution:)

http://forums.asp.net/t/1650984.aspx

http://www.quackit.com/javascript/javascript_print.cfm

on body load call the function as given in above link.

Thanks for your reply.

Actually I was using the same post for me references. But in this post there is  pop up Print Dialog Box which I want to omit. Is it possible to skip Print Dialog Box and print directly goes to my default printer.

scenario 

A person comes to attend the conference for which hew registered online

scans his confirmation #(barcode) and application reads all the sessions he opted for and diaplye all in the datalist.

Now I used javascript to print that specific section  of the web page

but when this happens the print dialog box appears (which I don't want it to appear).

Is it possible to send the command directly to printer when a person scans his barcode??

I just found an answer to it post at

http://forums.asp.net/t/1650984.aspx 

[RESOLVED] Can this button event be improved on

protected void btSearchDVR_Click(object sender, EventArgs e)
        {
            string add = txtAddress.Text;
            string city = txtCity.Text;
            string zip = txtZip.Text;
            string lnk = "http://www.site.com/sn/gpt.aspx?a" + HttpUtility.UrlEncode(add) + "&c=" + HttpUtility.UrlEncode(city) + "&z=" + HttpUtility.UrlEncode(zip);
            string results = null;


            try
            {
                WebClient client = new WebClient();
                results = client.DownloadString(lnk);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }


            string[] coord = results.Split(new char[] { ',' });
            string txtLat = coord[0];
            string txtLon = coord[1];


            ViewState["MethodName"] = "SearchResults";


                ObjectDataSource1.SelectMethod = "SearchResults";
                ObjectDataSource1.SelectParameters.Clear();


                if ((searchValue.Text == "") || (searchValue.Text == " "))
                {
                    ObjectDataSource1.SelectParameters.Add("searchValue", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("searchValue", TypeCode.String, searchValue.Text);
                }
                if (ddlDVR.SelectedValue == "0")
                {
                    ObjectDataSource1.SelectParameters.Add("dvrName", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("dvrName", TypeCode.String, ddlDVR.SelectedValue);
                }
                if ((txtSdate.Text == "") || (txtSdate.Text == " "))
                {
                    ObjectDataSource1.SelectParameters.Add("sdate", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("sdate", TypeCode.String, txtSdate.Text);
                }
                if ((ddlSHH.SelectedValue == "00") && (ddlSMM.SelectedValue == "00") && (ddlSSS.SelectedValue == "00"))
                {
                    ObjectDataSource1.SelectParameters.Add("stime", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("stime", TypeCode.String, ddlSHH.SelectedValue + ":" + ddlSMM.SelectedValue + ":" + ddlSSS.SelectedValue);
                }
                if ((txtEdate.Text == "") || (txtEdate.Text == " "))
                {
                    ObjectDataSource1.SelectParameters.Add("edate", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("edate", TypeCode.String, txtEdate.Text);
                }
                if ((ddlEHH.SelectedValue == "00") && (ddlEMM.SelectedValue == "00") && (ddlESS.SelectedValue == "00"))
                {
                    ObjectDataSource1.SelectParameters.Add("etime", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("etime", TypeCode.String, ddlEHH.SelectedValue + ":" + ddlEMM.SelectedValue + ":" + ddlESS.SelectedValue);
                }


                if (ddlvidpri.SelectedValue == "00")
                {
                    ObjectDataSource1.SelectParameters.Add("vidpri", TypeCode.String, null);
                }
                else
                {
                    ObjectDataSource1.SelectParameters.Add("vidpri", TypeCode.Int32, ddlvidpri.SelectedValue);
                }
                if (results != "Not Found")
                {
                    ObjectDataSource1.SelectParameters.Add("videoRAD", TypeCode.Decimal, ddlRadius.SelectedValue);
                    ObjectDataSource1.SelectParameters.Add("videoLat", TypeCode.Double, txtLat);
                    ObjectDataSource1.SelectParameters.Add("videoLon", TypeCode.Double, txtLon);
                }


                Session["ObjectDataSource1"] = ObjectDataSource1;


                if (ddlView.SelectedValue == "LV")
                {
                    GridView3.Visible = true;
                    GridView3.DataBind();
                    DataList1.Visible = false;
                }
                if (ddlView.SelectedValue == "IV")
                {
                    DataList1.Visible = true;
                    DataList1.DataBind();
                    GridView3.Visible = false;
                }
                lblResults.Visible = true;
                GridView1.Visible = false;
                Session["btClick"] = "1";
                searchNav5.Visible = true;
                graphic.Visible = false;


                dac.insertauditintosql(dac.usn, "Data Search", searchValue.Text);


                if ((GridView3.Rows.Count == 0) || (DataList1.Items.Count == 0))
                {
                    ddlView.Visible = false;
                    Btn1.Visible = false;
                }
                else
                {
                    ddlView.Visible = true;
                    Btn1.Visible = true;
                }
        }


 

Wherever you are checking for blank text from a textbox, you can use either String.IsNullOrEmpty or String.IsNullOrWhiteSpace functions.

 

ObjectDataSource1.SelectParameters.Add("searchValue",TypeCode.String,String.IsNullOrWhiteSpace(searchValue.Text)?null:searchValue.Text);
ObjectDataSource1.SelectParameters.Add("dvrName",TypeCode.String,ddlDVR.SelectedValue== 0"?null:ddlDVR.SelectedValue);
ObjectDataSource1.SelectParameters.Add("sdate",TypeCode.String,String.IsNullOrWhiteSpace(txtSdate.Text)?null:txtSdate.Text);
ObjectDataSource1.SelectParameters.Add("edate",TypeCode.String,String.IsNullOrWhiteSpace(txtEdate.Text)?null:txtEdate.Text);

 

 

[RESOLVED] How to define a DataList in the code behind (C#)

Hi,

I want to define a DataList in the code behind but i don't know how to do it.

I just just know how to do it in the ASP.NET:

Data Source DataList

<asp:AccessDataSource ID="adsExample" runat="server"
            DataFile="~/MyDatabase.mdb"
           
            SelectCommand="SELECT Best4.[Year], Countries.Flag, Countries.ENAME AS Champion FROM (Best4 INNER JOIN Champions ON Best4.CHID=Champions.CHID) INNER JOIN Countries ON Champions.CountryID=Countries.CountryID ORDER BY Best4.[Year];">
            </asp:AccessDataSource>

DataList

<asp:DataList ID="DataList1" runat="server" DataSourceID="adsExample">
            <ItemTemplate>
                <asp:Label ID="YearLabel" runat="server" Text='<%# Eval("Year") %>' />
                <asp:Image ID="FlagImage" runat="server" ImageUrl='<%# Eval("Flag") %>' />
                <asp:Label ID="ChampionLabel" runat="server" Text='<%# Eval("Champion") %>' />
                <br />
            </ItemTemplate>
        </asp:DataList>

When i am running my Visual Web Developper, the result is a list of x numbers of three values:

1999 [an image (Canadian Flag)] Canada

2003 [an image (US Flag)] USA

2005 [an image (Indian Flag)] India

...

 

I know how to create my Access Data Base source in C#, but i don't know how to define a DataList.

Can you help me please ?

Thank you very much for your attention.

Look at datasets and data tabeles in the System.data dll.  There is also a file called a dataset which you can set your connections and it will do the rest when you point it ot the DB and table or Stored Proc.  Basically it creates a class with methods to handle the data.  Look at the Web Forms section under data controls to learn more. 

Hi Phil,

You're concerned about creating a dynamic DataList control, so you will need to add a new class that inherite from ITemplate interface in aim to create an Item Template for your list.

you could test the following code:

classe< My Template: 

 

using System;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Test
{
    public class MyTemplate: ITemplate
    {
     //A variable to hold the type of ListItemType.
    ListItemType _templateType;
    //A variable to hold the column name.
    string _columnName;
    //Constructor where we define the template type and column name.
    public MyTemplate(ListItemType type, string colname)
    {
        //Stores the template type.
        _templateType = type;
        //Stores the column name.
        _columnName = colname;
    }
    void ITemplate.InstantiateIn(System.Web.UI.Control container)
    {
        switch (_templateType)
        {
         case ListItemType.Item:
                //Creates a new label control and add it to the container.
                Label tb1 = new Label();
                tb1.ID = "?";
                tb1.EnableViewState = true;
                tb1.DataBinding += new EventHandler(tb1_DataBinding);
                container.Controls.Add(tb1);
                break;
        }
    }
    void tb1_DataBinding(object sender, EventArgs e)
    {
        Label txtdata = (Label)sender;
         DataListItem container = (DataListItem)txtdata.NamingContainer;
        object dataValue = DataBinder.Eval(container.DataItem, _columnName);
        if (dataValue != DBNull.Value)
        {
            txtdata.Text = dataValue.ToString();
        }
    }
    }
}

 

Default Page :

using System;
using System.Web.UI.WebControls;

namespace Test
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataList datalist = ConstructDataList("Year");
            // declare the Source provider
            AccessDataSource aDS = new AccessDataSource();
            aDS.ID = "adsExample";
            aDS.DataFile = "~/MyDatabase.mdb";
            aDS.SelectCommand = "SELECT Best4.[Year], Countries.Flag, Countries.ENAME AS Champion FROM (Best4 INNER JOIN Champions ON Best4.CHID=Champions.CHID) INNER JOIN Countries ON Champions.CountryID=Countries.CountryID ORDER BY Best4.[Year];";
           // populate the data
            datalist.DataSourceID = "adsExample";
            datalist.DataBind();
            PlaceHolder1.Controls.Add(datalist);
        }

        
        public DataList ConstructDataList(string field)
        {
            DataList dataList = new DataList();
            dataList.ItemTemplate = new MyTemplate(ListItemType.Item, field);
            return dataList;
        }
    }
}