我正在使用我的格式的数据网格,它有2个按钮,编辑和删除,但这些事件并没有被触发。我在代码中哪里出错了。我在vb.net中运行相同的代码,但在c#中转换后,它无效。 Plz帮助。
在将代码从vb.net转换为c#时,我是否还需要进行其他更改? 提前谢谢!
private void dgjob_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
switch (e.CommandName)
{
case "edit":
try
{
qry = "select ROLE from AV_USER_DETAILS where USER_ID='" + Session["user_name"] + "'";
cmd.Connection = con;
cmd.CommandText = qry;
con.Open();
if (Information.IsDBNull(cmd.ExecuteScalar()))
{
av_class.alert("User Not Permitted ", this);
con.Close();
return;
}
string st = cmd.ExecuteScalar().ToString();
if (!(st == "hod") & !(st == "Media_Planner"))
{
av_class.alert("User Not Permitted ", this);
con.Close();
return;
}
con.Close();
if (st == "hod")
{
goto mygo;
}
else
{
if (st == "Media_Planner")
{
goto mygo;
}
else
{
av_class.alert("User not Permitted", this);
return;
}
}
}
catch (Exception ex)
{
av_class.alert("Error .", this);
}
mygo:
ViewState["job_code"] = bll.RemoveSpecialChars(((Label)e.Item.FindControl("lbljobcode")).Text.Trim());
//4 tables changed av_creative_for_agency,av_datatable_one_for_ro,av_cmp_prd,av_datatable_two_for_ro used only once'
DataTable dat = new DataTable();
DAL o = new DAL();
dat = o.GetDataForGrid("select top 1 job_code from av_creative_for_agency where job_code='" + ViewState["job_code"] + "'");
if (dat.Rows.Count > 0)
{
av_class.alert("Ro's are Created for this !So can't be Edited", this);
return;
}
if (chk_job(ViewState["job_code"].ToString()) == false)
{
//alert("This Job already confirmed, so cannot be edited!", Me)
//Exit Sub
tb_campaign.Enabled = false;
}
retrive_from_DB(bll.RemoveSpecialChars(ViewState["job_code"].ToString()));
tabjob.Visible = true;
break;
Case "delete"
ViewState("job_code") = bll.RemoveSpecialChars(CType(e.Item.FindControl("lbljobcode"), Label).Text.Trim)
If chk_job(ViewState("job_code")) = False Then
alert("This Job already confirmed, so cannot be deleted!", Me)
Exit Sub
End If
If del_draft(ViewState("job_code")) = False Then
alert("Error in processing!", Me)
Exit Sub
End If
tabjob.Visible = False
alert("Draft deleted successfully!", Me)
}
}
catch (Exception ex)
{
}
}
ASPX CODING OF THE DATAGRID
<asp:datagrid id="dgjob" runat="server" CssClass="davpdgdatagrid"
PageSize="100" AutoGenerateColumns="False" ShowFooter="True"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal"
HorizontalAlign="Center">
<AlternatingItemStyle CssClass="davpdgalt"></AlternatingItemStyle>
<ItemStyle CssClass="davpdgitem" HorizontalAlign="Center"></ItemStyle>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle Wrap="False" HorizontalAlign="Center" CssClass="davpdgheader"
BackColor="#333333" Font-Bold="True" ForeColor="White"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button id="btnedit" runat="server" CssClass="btn btn-primary" Text="Edit" CausesValidation="False"
CommandName="edit"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button id="btndelete" runat="server" CssClass="btn btn-primary" Text="Delete" CausesValidation="False"
CommandName="delete"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Job Code">
<ItemTemplate>
<asp:Label id="lbljobcode" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.job_code") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="TextBox2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.job_code") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="job_name" HeaderText="Job Name"></asp:BoundColumn>
<asp:BoundColumn DataField="job_date" HeaderText="Job Date"></asp:BoundColumn>
</Columns>
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" Mode="NumericPages" />
<SelectedItemStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
</asp:datagrid>
答案 0 :(得分:0)
在您的aspx文件中,您无法调用“dgjob_ItemCommand”方法,因为您希望事件被触发。
你必须在Grid的OnSelectedIndexChanged事件上调用“dgjob_ItemCommand”。
<asp:datagrid id="dgjob" runat="server" CssClass="davpdgdatagrid"
PageSize="100" AutoGenerateColumns="False" ShowFooter="True"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"
BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal"
HorizontalAlign="Center" OnSelectedIndexChanged="dgjob_ItemCommand">
<AlternatingItemStyle CssClass="davpdgalt"></AlternatingItemStyle>
我希望这会有所帮助。