have to click 'edit' button twice for event handler to fire.

Joined
Jan 28, 2008
Messages
2
Reaction score
0
Hi,
i have a form with drop down boxes containing territories and agents. once an agent is selected a table is built dynamically and at the end of each row is and 'edit' button to edit the row. This works fine except when a territory has only one agent, it automatically makes the table for that agent as soon as the territory is selected, and then you have to click the edit button twice to edit the row. Someone else wrote most of this code so please help with detailed instructions on how to fix it. I am copying what i think os the relevent portion of the code here -

public class EnhancedLinkButton : System.Web.UI.WebControls.LinkButton
{ public EnhancedLinkButton(int Month)
{ base.Text = "Edit";
month = Month;
}
private int month;
public int Month { get { return month; } set { month = value; } }
}
protected void Page_Load(object sender, EventArgs e)
{ try
{ // To clear the table when the agents name is changed
tblData.Rows.Clear();

if (Session["Year"] == null)
Session["Year"] = System.DateTime.Now.Year;
if (!IsPostBack)
{
FillRegions();
//if there is only one item in select box, fire selected index change event
if (ddlRegion.Items.Count == 1)
ddlRegion_SelectedIndexChanged(null, EventArgs.Empty);
}
if (ddlAgent.SelectedIndex > 0 || ddlAgent.Items.Count == 1)
DrawGrid();
trVolGoal.Visible = trMerGoal.Visible = trAGMGoal.Visible = trUpdate.Visible = false;
}
catch (System.Exception exc)
{
ErrorLog.Log(exc);
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
string Volume, Merchants, AGM;
string sql;
RemoteLynkData rldo;
try
{
Volume = txtVol.Text;
Merchants = txtMer.Text;
AGM = txtAGM.Text;
EditDrawnGrid((int)Session["CurrentMonth"]);
//validate inputs and give real error messages
sql = "EXEC uspGT_SetGoal '" + Context.User.Identity.Name + "', "+ddlAgent.Items[ddlAgent.SelectedIndex].Value+
", " + Session["CurrentMonth"].ToString() + "," + Session["Year"].ToString() +
"," + Merchants.Replace(",", "") + "," + Volume.Replace(",", "") +
"," + AGM.Replace(",", "");
rldo = new RemoteLynkData();
rldo.ExecuteNoQuery(sql, "LaunchPad");
EnhancedLinkButton_Click(tblData.Rows[(int)Session["CurrentMonth"]].Cells[7].Controls[0], new EventArgs());
}
catch (System.Exception exc)
{
ErrorLog.Log(exc);
}
}
protected void ddlTerritory_SelectedIndexChanged(object sender, EventArgs e)
{
try
{ddlAgent.Items.Clear();
FillAgent();

//if there is only one item in select box, draw grid
if (ddlAgent.Items.Count == 1)
ddlAgent_SelectedIndexChanged(null,EventArgs.Empty);

}
catch (System.Exception exc)
{
ErrorLog.Log(exc);
}
}
protected void ddlAgent_SelectedIndexChanged(object sender, EventArgs e)
{
try
{ if (ddlAgent.SelectedIndex > 0)
EditDrawnGrid(1);

}
catch (System.Exception exc)
{
ErrorLog.Log(exc);
}
}
protected void EnhancedLinkButton_Click(object sender, EventArgs e)
{
try
{ EditDrawnGrid(((EnhancedLinkButton)sender).Month);

}
catch (System.Exception exc)
{
ErrorLog.Log(exc);
}
private void DrawGrid()
{
DataSet ds;
try
{ string sql1 = "EXEC uspGT_GetGoalDataYear " +
DropDownYearList.Items[DropDownYearList.SelectedIndex].Text + ", " +
ddlRegion.Items[ddlRegion.SelectedIndex].Value + ", " +
ddlDistrict.Items[ddlDistrict.SelectedIndex].Value + ", " +
ddlTerritory.Items[ddlTerritory.SelectedIndex].Value + ", " +
ddlAgent.Items[ddlAgent.SelectedIndex].Value;

RemoteLynkData rldo = new RemoteLynkData();
ds = rldo.GetDataSet(sql1, "LaunchPad");

Session["Year"] = int.Parse(DropDownYearList.Items[DropDownYearList.SelectedIndex].Value);

if (ds.Tables[0].Rows.Count == 12)
{ tblData.Rows.Clear();
tblData.Rows.Add(GetHeaderRow());
foreach (DataRow dr in ds.Tables[0].Rows)
tblData.Rows.Add(GetRow(dr));
trData.Visible = true;
}
else
throw new Exception("Invalid Dataset returned from database. SQL:" + sql1);

for (int i = 0; i <= DropDownYearList.Items.Count - 1; i++)
{if (Session["Year"].ToString() == DropDownYearList.Items.Text)
{
DropDownYearList.Items.Selected = true;
break;
}
}

}
catch (System.Exception exc)
{
ErrorLog.Log(exc);
}
}
private void EditDrawnGrid(int Month)
{
try
{ //set style sheet
Session["CurrentMonth"] = Month;

//load text fields
DataSet ds = new DataSet();
string sql = "EXEC [uspGT_GetGoalDataMonth] " + ddlAgent.Items[ddlAgent.SelectedIndex].Value +
", " + Month.ToString() + ", " + DropDownYearList.Items[DropDownYearList.SelectedIndex].Text + "";
RemoteLynkData rldo = new RemoteLynkData();
ds = rldo.GetDataSet(sql, "LaunchPad");
if (ds.Tables[0].Rows.Count == 1)
{
txtAGM.Text = ds.Tables[0].Rows[0]["AGMGoal"].ToString();
txtMer.Text = ds.Tables[0].Rows[0]["MerchantGoal"].ToString();
txtVol.Text = ds.Tables[0].Rows[0]["VolumeGoal"].ToString();
}
else if (ds.Tables[0].Rows.Count == 0)
txtAGM.Text = txtMer.Text = txtVol.Text = "";
else
throw new Exception("Invalid Dataset returned from database. SQL:" + sql);

for (int i = 1; i <= tblData.Rows.Count - 1; i++)
{
for (int j = 0; j <= 7; j++)
{
if (i == Month)
{
//edit
if (tblData.Rows.Cells[j].CssClass.ToUpper().EndsWith("LEFT"))
tblData.Rows.Cells[j].CssClass = "dataCellEditLeft";
else
tblData.Rows.Cells[j].CssClass = "dataCellEditRight";

if (ds.Tables[0].Rows.Count == 1)
{
switch (j)
{
case 2:
tblData.Rows.Cells[j].Text = ((decimal)ds.Tables[0].Rows[0]["VolumeGoal"]).ToString("C");
break;
case 3:
tblData.Rows.Cells[j].Text = ds.Tables[0].Rows[0]["MerchantGoal"].ToString();
break;
case 4:
tblData.Rows.Cells[j].Text = ((decimal)ds.Tables[0].Rows[0]["AGMGoal"]).ToString("C");
break;
case 5:
tblData.Rows.Cells[j].Text = ((DateTime)ds.Tables[0].Rows[0]["LastUpdate"]).ToShortDateString();
break;
case 6:
tblData.Rows.Cells[j].Text = ds.Tables[0].Rows[0]["UpdateUser"].ToString();
break;
default:
break;
}
}
}
else
{ if (tblData.Rows.Cells[j].CssClass.ToUpper().EndsWith("LEFT"))
tblData.Rows.Cells[j].CssClass = "dataCellLeft";
else
tblData.Rows.Cells[j].CssClass = "dataCellRight";
}
}
}

trVolGoal.Visible = trMerGoal.Visible = trAGMGoal.Visible = trUpdate.Visible = true;
}
catch (System.Exception exc)
{
ErrorLog.Log(exc);
}
}

Any help will be greatly appreciated.
Thanks.
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top