Monday, March 12, 2012

How to handle linkbutton click?

guys I've met with a problem

I need to create a linkbutton control dynamicaly within a repeater

Dim X As New RepeaterItem(0, ListItemType.Item)

Dim LB As New LinkButton
LB.Text = SomeText
LB.CommandArgument = Value
LB.CausesValidation = True
AddHandler LB.Click, AddressOf PageClick
X.Controls.Add(LB)


Rep.Controls.Add(X)

...


Protected Sub PageClick(ByVal sender As Object, ByVal e As EventArgs)

Search(3, TryCast(sender, LinkButton).CommandArgument)

End Sub

but when I click that linkbutton it doesn't fire. what I do wrong?

thanks

Make sure you are re-creating this LinkButton each and every PostBack of the Page.


Add a command name to your LinkButton something like "MyLinkButton" then in the ItemCommand repeater event check if the args, hang on I will put up a sample.

ProtectedSub rptMyRepeater_ItemCommand(ByVal sourceAsObject,ByVal eAs System.Web.UI.WebControls.RepeaterCommandEventArgs)Handles rptMyRepeater.ItemCommand

If e.CommandName ="MyLinkButton"Then
' the link button was clicked
EndIf

EndSub


Eah, I need to recreate them, it works now. Is there a way to avoid recreation on every postback?


Sure, if you wanted, you could always have some type of flag stored in your Page's ViewState collection which you can use to re-create your controls or not. However, no re-creation, no event handlers.

Labels: , , , , , , , , , , , , , , ,

How to handle DBNull DataType for Ms access ??

Hi,

I have problem in using this function to check if the field is empty or null :

Let say , I have connect to a Database and set up Dataset :

Dim Ds as dataset

Dim StrAddr as string

If IsDBNull( DS.tables("tblName").rows(0).item("Address")) then

LbMsg.text =" No address entered."

else

StrAddr = DS.tables("tblName").rows(0).item("Address"))

end if

I have error msg : Can not cast DBNull to string.

All I wanted is to check the field address to see if it is empty or no data entered.

Please help.

How to check ? How to handle if the field has nothing in it at all. How to use IsDBNull() ?

Thanks in advance.

Try this:

StrAddr = DS.tables("tblName").rows(0).item("Address").ToString

If StrAddr ="" then StrAddr = "No Address Entered"

Tim

Labels: , , , , , , , , , , , , ,