LinkButton Issue!

  • Thread starter Steve Harclerode
  • Start date
S

Steve Harclerode

Here's some code that I'm using to create a simple list of logical drive
letters (my web application impersonates a user). I'm finding that my event
handler doesn't fire. Can anyone give me a hint as to why it doesn't work?
It's nearly a copy of some sample code that I lifted from a friend, whose
code works (so he says!). By the way, I'm using a dotnet Table control in
the ASPX page which starts off empty, it's ID is dirTable.

By the way, I have a co-worker who had the same issue a year ago, and he
ended up associating client side JavaScript which set a hidden field, and
then called submit().

----------------------

private void Page_Load(object sender, System.EventArgs e)
{
if ( !IsPostBack )
{
SetupDriveList();
}
}

void SetupDriveList()
{
string[] drives = Directory.GetLogicalDrives();
for (int i = 0; i < drives.Length; i++)
{
if ( drives[0] == 'A' || drives[0] == 'B' )
{
continue;
}
TableRow tr = new TableRow();

// create link button for drive letter
LinkButton lb = new LinkButton();
lb.Text = drives;
lb.CommandName = "drive";
lb.CommandArgument = drives[0].ToString();
lb.Command += new CommandEventHandler( OnClickLink );
lb.Attributes.Add("onclick", "OnClickLink" );

// add link button to table cell
TableCell td = new TableCell();
td.Controls.Add( lb );

// add table cell to table row
tr.Cells.Add( td );

// add table row to table
dirTable.Rows.Add( tr );
}
}

void OnClickLink(object O, CommandEventArgs e)
{
string commandName = e.CommandName.ToString();
string commandArg = e.CommandArgument.ToString();
}
 
J

Jared

Steve,
This is just a guess, but I don't think you need to append the onClick
attribute to a link button web control unless you are trying to run client
side code. ASP .NET renders the control as an anchor <a> element that is set
to post back to the sever with the controls arguments. View the source of
the control should be something similar to:

<a href="javascript:__doPostBack('_ctl0','')">My Link Button</a>

Try to remove/remark out the following line and see if your handler fires.

lb.Attributes.Add("onclick", "OnClickLink" );

Jared
 
G

Guest

Steve,

The order of events is Page Load --> Process Events (e.g. button click) -->
Page Render. Because you create your buttons dynamically, and *only* when
it's not a postback, the buttons aren't there the next time around to get the
event. You need to either rebuild them each time, or store them in viewstate,
or instead of rolling the table yourself use a repeater/datalist/datagrid in
which case the viewstate save/fetch will be handled for you.

hth,

Bill
 
C

Chad

I have what appears to be the same problem.

I realized that I needed to recreate the screen add re-add the controls
before the events for the controls would fire. However, they only fire every
other submit!




Bill Borg said:
Steve,

The order of events is Page Load --> Process Events (e.g. button click) -->
Page Render. Because you create your buttons dynamically, and *only* when
it's not a postback, the buttons aren't there the next time around to get the
event. You need to either rebuild them each time, or store them in viewstate,
or instead of rolling the table yourself use a repeater/datalist/datagrid in
which case the viewstate save/fetch will be handled for you.

hth,

Bill

Steve Harclerode said:
Here's some code that I'm using to create a simple list of logical drive
letters (my web application impersonates a user). I'm finding that my event
handler doesn't fire. Can anyone give me a hint as to why it doesn't work?
It's nearly a copy of some sample code that I lifted from a friend, whose
code works (so he says!). By the way, I'm using a dotnet Table control in
the ASPX page which starts off empty, it's ID is dirTable.

By the way, I have a co-worker who had the same issue a year ago, and he
ended up associating client side JavaScript which set a hidden field, and
then called submit().

----------------------

private void Page_Load(object sender, System.EventArgs e)
{
if ( !IsPostBack )
{
SetupDriveList();
}
}

void SetupDriveList()
{
string[] drives = Directory.GetLogicalDrives();
for (int i = 0; i < drives.Length; i++)
{
if ( drives[0] == 'A' || drives[0] == 'B' )
{
continue;
}
TableRow tr = new TableRow();

// create link button for drive letter
LinkButton lb = new LinkButton();
lb.Text = drives;
lb.CommandName = "drive";
lb.CommandArgument = drives[0].ToString();
lb.Command += new CommandEventHandler( OnClickLink );
lb.Attributes.Add("onclick", "OnClickLink" );

// add link button to table cell
TableCell td = new TableCell();
td.Controls.Add( lb );

// add table cell to table row
tr.Cells.Add( td );

// add table row to table
dirTable.Rows.Add( tr );
}
}

void OnClickLink(object O, CommandEventArgs e)
{
string commandName = e.CommandName.ToString();
string commandArg = e.CommandArgument.ToString();
}
 
S

Steve Harclerode

Nope, that didn't do it. That's how it was before, I just forgot to remove
that code from my post. Oops.

Thanks for the reply.

- Steve
 
S

Steve Harclerode

Thanks, I didn't realize you could store that kind of object in the
Viewstate.

- Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top