ponedjeljak, 21. rujna 2009.

User Control - Adding Events

1. define the events in code behind of user control
(Remember: To define an event you must use the event keyword with a delegate that represents the signature of the event. )

public event EventHandler SelectedIndexChanged;

2. Parameters

public string param_odabranaVrijednost
{
get { return odabranaVrijednost; }
//set { odabranaVrijednost = value; }
}

3.

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedID = GridView1.SelectedValue.ToString(); //first dataKey "Location_ID"
string selectedDiggins = GridView1.DataKeys[GridView.SelectedIndex].Values["Diggings"].ToString();
// finding dataKey by name

odabranaVrijednost = "Odabrani id je : " + selectedID.ToString();

if (SelectedIndexChanged != null)
{
SelectedIndexChanged(this, EventArgs.Empty);
}
}

4. Saving value in some text box inside page or control that uses user control

protected void SelectedIndexChanged(object sender, EventArgs e)
{
txtLokacija.Text = Lokacija1.param_odabranaVrijednost;
}

5. In designe mode .ascx or .aspx call eventhandler inside of used control

uc2:Lokacija ID="Lokacija1" runat="server" OnSelectedIndexChanged="SelectedIndexChanged" //moras napisat rucno OnSelectedIndexChanged jer je ovo kreirani event pa ga nema u inteli sensu