Peter Chan
asked on
Unable to edit DDL of the row
Hi,
I have the codes like
I have the codes like
<asp:GridView ID="GridView1" runat="server" Width = "550px"
AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green" AllowPaging ="true" Data
ShowFooter = "true" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" PageSize = "10" OnRowDeleting="GridView1_RowDeleting" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Ite">
<ItemTemplate>
<asp:DropDownList ID="ddl" OnSelectedIndexChanged="ddl_IndexChanged" AutoPostBack="true" runat="server">
</asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl2" OnSelectedIndexChanged="ddl_IndexChanged" AutoPostBack="true" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddl3" OnSelectedIndexChanged="ddl_IndexChanged" AutoPostBack="true" runat="server">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:TextBox id="tb_Ite_code" runat="server" Text='<%# Eval("Ite_code") %>' Visible="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="TextBox2" runat="server" Text='<%# Eval("Ite_code") %>' Visible="false" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="TextBox5" runat="server" Text='<%# Eval("Ite_code") %>' Visible="false" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "150px" HeaderText = "Rate">
<ItemTemplate>
<asp:Label ID="curr_rate" runat="server"
Text='<%# Eval("Ite_rate")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Eval("Ite_rate")%>' >
</asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%# Eval("Ite_rate")%>' >
</asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<span onclick="return confirm('Are you sure to delete the record?')">
<asp:LinkButton CssClass="forbutton" ID="lnkdelete" runat="server" Text="Delete" ForeColor="Red" CommandName="Delete"/>
</span>
</ItemTemplate>
<FooterTemplate>
<asp:Button CssClass="forbutton" ID="btnadd" runat="server" Text="Add" OnClick="AddNewRec" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
</Columns>
<AlternatingRowStyle BackColor="White"/>
<EditRowStyle BackColor="#efefef" />
<FooterStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="true" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
...
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
/*DataTable tab1 = new DataTable();
DataColumn intid = new DataColumn("int_id");
DataColumn intdesc = new DataColumn("currency_code");
tab1.Columns.Add(intid);
tab1.Columns.Add(intdesc);
DataSet ds = new DataSet();*/
//ds = GetDataSet();
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl2");
if (ddl == null)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoB", "javascript:alert('Unable to bind Edit row in Grid.');", true);
//lb_msg.Text = "Unable to bind Edit row in Grid.";
return;
}
else
this.BindDropDown(ddl);
Label lb_rt = (Label)e.Row.FindControl("curr_rate");
if (Session["status"] != "Edit")
ddl.Enabled = false;
else
ddl.Enabled = true;
}
else
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
if (ddl == null)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoB", "javascript:alert('Unable to bind row in Grid.');", true);
//lb_msg.Text = "Unable to bind row in Grid." + Convert.ToString(e.Row.RowState);
return;
}
else
this.BindDropDown(ddl);
Label lb_rt = (Label)e.Row.FindControl("curr_rate");
if (Session["status"] != "Edit")
ddl.Enabled = false;
else
ddl.Enabled = true;
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl3");
if (ddl == null)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "InfoB", "javascript:alert('Unable to bind footer row in Grid.');", true);
//lb_msg.Text = "Unable to bind footer row in Grid.";
return;
}
else
this.BindDropDown(ddl);
Label lb_rt = (Label)e.Row.FindControl("curr_rate");
if (Session["status"] != "Edit")
ddl.Enabled = false;
else
ddl.Enabled = true;
}
}
...
I don't know why I can't edit the DDL of 1st field on Gridview, when I click "Edit" to the row.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry, please disregard my last reply.
if (Session["status"] != "Edit")
ddl.Enabled = false;
else
ddl.Enabled = true;
Your code checks for the "Edit" value in the session variable 'status'. If it is "Edit", it enables it.
So, you are somewhere setting the value of 'status' variable. Debug and see if the Session["status"] gives the value "Edit"
ddl.Enabled = false;
else
ddl.Enabled = true;
Your code checks for the "Edit" value in the session variable 'status'. If it is "Edit", it enables it.
So, you are somewhere setting the value of 'status' variable. Debug and see if the Session["status"] gives the value "Edit"
ASKER
Sorry, once I've selected one value to that DDL, I get this
while this is the event
Server Error in '/Start_dev' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Start._Default.ddl_IndexChanged(Object sender, EventArgs e) +143
System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +129
System.Web.UI.Page.RaiseChangedEvents() +332
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3571
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34237
while this is the event
protected void ddl_IndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (sender as DropDownList);
GridViewRow row = ddl.NamingContainer as GridViewRow;
TextBox tb2 = row.FindControl("tb_ite_code") as TextBox;
tb2.Text = ddl.SelectedItem.Text;
}
...
why?
Your code is not finding the DropDownList. What event is this code attached to?
ASKER
Sorry, I click the DDL of 1st field of Gridview and the current error comes out. Why?
did you write the code for ddl_IndexChanged? if not write the code for it and check
ASKER
One last thing, here is Gridview
<asp:GridView ID="GridView1" runat="server" Width = "550px"
AutoGenerateColumns = "false" Font-Names = "Arial"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"
HeaderStyle-BackColor = "green" AllowPaging ="true" Data
ShowFooter = "true" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" PageSize = "10" OnRowDeleting="GridView1_RowDeleting" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Currency">
<ItemTemplate>
<asp:DropDownList ID="ddl" OnSelectedIndexChanged="ddl_IndexChanged" AutoPostBack="true" runat="server">
</asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl2" OnSelectedIndexChanged="ddl2_IndexChanged" AutoPostBack="true" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddl3" OnSelectedIndexChanged="ddl3_IndexChanged" AutoPostBack="true" runat="server">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:TextBox id="tb_currency_code" runat="server" Text='<%# Eval("currency_code") %>' Visible="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="tb_curr_cd" runat="server" Text='<%# Eval("currency_code") %>' Visible="false" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox id="tb_curr_cd2" runat="server" Text='<%# Eval("currency_code") %>' Visible="false" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "150px" HeaderText = "Saving Rate">
<ItemTemplate>
<asp:Label ID="curr_rate" runat="server"
Text='<%# Eval("Currency_rate")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="curr_rate" runat="server"
Text='<%# Eval("Currency_rate")%>' >
</asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="curr_rate2" runat="server"
Text='<%# Eval("Currency_rate")%>' >
</asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<span onclick="return confirm('Are you sure to delete the record?')">
<asp:LinkButton CssClass="forbutton" ID="lnkdelete" runat="server" Text="Delete" ForeColor="Red" CommandName="Delete"/>
</span>
</ItemTemplate>
<FooterTemplate>
<asp:Button CssClass="forbutton" ID="btnadd" runat="server" Text="Add" OnClick="AddNewRec" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
</Columns>
<AlternatingRowStyle BackColor="White"/>
<EditRowStyle BackColor="#efefef" />
<FooterStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="true" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="true" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
...
and here is the event to update the record protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
TextBox tb_currency_code = row.FindControl("tb_curr_cd") as TextBox;
Label curr_rate = row.FindControl("curr_rate") as Label;
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Mssqlconn2"].ConnectionString))
//using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Jet OLEDB:Database Password=213461;Data Source=C:\inetpub\VS2012\DB1.accdb"))
{
string sql = "Update [savingrate] set [Currency_rate]= @Currency_rate" + " where [currency_code]=@currency_code";
using (SqlCommand cmd = new SqlCommand(sql, conn))
//using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
cmd.Parameters.AddWithValue(
"@currency_code", tb_currency_code.Text.Trim());
cmd.Parameters.AddWithValue(
"@Currency_rate", Convert.ToDecimal(curr_rate.Text));
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
lb_msg.Text = "Record updated successfully!";
GridView1.EditIndex = -1;
this.PopulateData();
}
...
When I press Update to the row, how to ensure that the DDL is showing the current value I've selected, as now it is not?
ASKER