• Feed RSS
I was working on a uploading script in asp.net. In asp.net its straightforward & easy and also I am not writing on uploading issue. Its about image refresh. When I click upload, it causes a postback, uploads the selected image , but on page, it still remains the past image. To recover from this problem, you have to do a simple thing, wither from server site script or client site(javascript). In asp.net/c#, its as like follows:

imgLogo.Src = "~/pages/images/testLogo.gif?"+DateTime.Now.Second.ToString();

call this after the uploading script. For doing this in javascript, simply call a function on page load which will set the src attribute like as follow:

tmp = new Date();
imageUrl = "pages/images/testLogo.gif?"+tmp.getTime().toString();
document.getElementById('imgLogo').src = imageUrl;

You will always get updated image. This appended timestamp causes browser each time to load the image again rather than load from cache.

Sometimes, it may be a requirements to create dynamic asp.net controls to be added on page. Gridview is such one control that is often required to show data in a defined format. Here is the code for this to be accomplished.

//create a gridview object
dGv = new GridView();

//create a template field to contain the checkbox control
TemplateField template = new TemplateField();

//mytemplate is customized class for adding checkbox that implements ITemplate(required)
template.ItemTemplate = new MyTemplate();
dGv.Columns.Add(template);

//create a button & add it to gridview
ButtonField btn = new ButtonField();
btn.ButtonType = ButtonType.Button;
btn.Text = "test button";
dGv.Columns.Add(btn);

//get a simple table to be bound
dGv.DataSource = MyTable.SimpleTable();
dGv.DataBind();

//add gridview to page
Page.Form.Controls.AddAt(0, dGv);

//add an EventListener after postback
dGv.RowCommand += dgv_RowCommand;

Here MyTable.SimpleTable() returns a data table. it may either a data table from your database or a dynamically & melodramatically created.

A little tricky part is the itemp template creation. You need to creat your own class that implements ITemplate(an interface). Here is a simple example:

public class MyTemplate : ITemplate
{
public MyTemplate()
{
}

//required function to be implemented
public void InstantiateIn(Control control)
{
//add the checkbox to template
CheckBox chkbx = new CheckBox();
chkbx.ID = "chkbx";
control.Controls.Add(chkbx);
}
}

If you have a button/url link on your grid (in this example, a button column exist), the last line

dGv.RowCommand += dgv_RowCommand;

adds a handler. You can handle it as follows:

protected void dgv_RowCommand(object sender, GridViewCommandEventArgs e)
{
//get the row no that caused the postback
int i = Convert.ToInt32(e.CommandArgument);

//check each checkbox whether they are checked
int count = 0;
string test = "";

//iterate every row
foreach (GridViewRow row in dGv.Rows)
{
//work with controls & cells
CheckBox temp = row.FindControl("chkbx") as CheckBox;
test = row.Cells[0].Text;
if(temp.Checked)
{
count++;
}
}
}

You can extend this simple example to fullfill your needs.


Now a days, flash contact form are very much available. The UI,captcha etc are made are made in flash, validation can also be done using actionscript without any help of javascript and then upon a submit button click, flash submits the data to a server file(like php). I was doing exacltly same thing. Then I fell in a problem with flash - php communication. To send data from flash to php we, usually use

loader.dataFormat = URLLoaderDataFormat.VARIABLES;

where loader is an instance of URLLoader Class. Sometimes it works, sometimes not. SO, many developers got confused.

In some cases, one problem occurs(That I faced), when php sends back the response to flash(as an acknowledgment whether data was submitted successfully or not), it encode the data in variables format, some of them, flash/actionscript can't understand. So, its better to use URLLoaderDataFormat.text than the above to make the communication bug free. This is usually helpfull only when you are communicate from flash to php and most of the times, we need communication in this direction.