Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

programmatically adding an edit column

Status
Not open for further replies.

link9

Programmer
Nov 28, 2000
3,387
US
Hello all --

Making a first shot at adding an edit column through code, and can't seem to get the event handler to fire.

The following is my datagrid declaration:
Code:
<asp:datagrid id=&quot;dgUsers&quot; runat=&quot;server&quot; OnEditCommand=&quot;DataGrid_Edit&quot; OnDeleteCommand=&quot;DataGrid_Delete&quot; />
and here's the code that adds the edit column:
Code:
Dim editColumn As New System.Web.UI.WebControls.EditCommandColumn()
editColumn.HeaderText = &quot;Edit&quot;
editColumn.EditText = &quot;Edit&quot;
editColumn.ButtonType = ButtonColumnType.LinkButton
dgUsers.Columns.Add(editColumn)

However, the DataGrid_Edit function is not fired when someone clicks the edit button. However, it is fired if I add it like so to the declaration:
Code:
<asp:datagrid id=&quot;dgUsers&quot; runat=&quot;server&quot;  OnEditCommand=&quot;DataGrid_Edit&quot; OnDeleteCommand=&quot;DataGrid_Delete&quot;>
<Columns>
<asp:EditCommandColumn ButtonType=&quot;LinkButton&quot;  EditText=&quot;Edit&quot; HeaderText=&quot;Edit&quot; />
</Columns>
</asp:datagrid>

Anyone see what I'm doing wrong here? What must I do to wire the event up?

Thanks! :)
paul
penny1.gif
penny1.gif
 
You might want to try to dynamically set up the onclick attribute also. Something like this:

System.Web.UI.WebControls.EditCommandColumn()
Dim editColumn As linkbutton
editColumn = new linkbutton()
editColumn.HeaderText = &quot;Edit&quot;
editColumn.EditText = &quot;Edit&quot;
editColmn.Attributes.Add(&quot;onclick&quot;, &quot;Do_Something&quot;)
dgUsers.Columns.Add(editColumn)

Hope this helps.

Regards,
NatGreen
 
on the first datagrid you have to make sure you have a button that fires that edit function:

<asp:button ID=&quot;button1&quot; RunAt=&quot;server&quot; CommandName=&quot;Edit&quot; />

that CommandName is picked up by the DataGrid in the OnEditCommand=&quot;DataGrid_Edit&quot;

hth
 
Thanks both of you, but:

Nat, editColumn should be an edit column, not a linkButton, and when it is an editColumn, it has no .attributes property -- and I can't get explicitly to the button's properties to set any onClick event (nor should I have to, since if I get the event wired to the datagrid properly, it should just fire)

Alcar, when you add an edit column to a datagrid, you just need to supply the editText property, and it adds the button for you. No need to specify the &quot;Edit&quot; on an &quot;editColumn&quot;. And as far as that goes, as for my delete command (which isn't listed here), I have specified the commandName property as &quot;delete&quot; and yet, it still won't fire the event I have specified in the onDeleteCommand property of the datagrid (which I also omitted here for simplicity's sake).

There has to be something that I'm missing in the wiring of an event programmatically, instead of declaratively.

Anyone else?

:)
paul
penny1.gif
penny1.gif
 
There's a Private WithEvents added to the code behind file for your datagrid? If not, the datagrid may not be able to access the Delete command sub?

Just throwing this out.
:)

Jack
 
Yes... the declaration is there --

Actually, the code does add the edit column to the datagrid (proving that not only is it there, but it's working)... it's just that the edit event that I've declared won't fire when I add the column through code.
penny1.gif
penny1.gif
 
Ok... my workaround for this problem was to leave the columns in there declaratively in the aspx file, and then hide them programmatically when I didn't want them in there:

dgUsers.columns(index).visible = false

I'm still all ears on how to get this to work, however, if someone has a bright idea. It simply MUST be possible -- everything is supposed to be able to be accomplished through pure code if that's what you like to do. I'm just missing something.

I'll post back when I figure it out.

:)
paul
penny1.gif
penny1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top