lpparker
asked on
Changing an ImageButton CommandName located in a DataList Using C#
Hi, I asked this question before but did not get an answer in C#.
I have a LinkButton in the HeaderTemplate of a DataList.
When you click it, I want it to change the CommandName of a ImageButton in the same DataList Control located in it's ItemTemplate.
How do I do this in C#? I get an error on line 3 of this code.
Thanks for your help.
LPP
========================== ========== ========== ==========
void TestDataBound (object sender, DataListCommandEventArgs e) {
ImageButton ib = e.Item.FindControl("editte mplate") as ImageButton;
Response.Write(ib.CommandN ame); //<<----------Object reference not set to an instance of an object.
Response.End();
}
</script>
<html>
<body>
<form id="myform" name="myform" runat="server">
<asp:DataList ID="dl1" ShowHeader="true" runat="server" ExtractTemplateRows="false " OnItemCommand="TestDataBou nd">
<headertemplate>
<asp:LinkButton ID="linkbutton" runat="server" CommandName="Test" Text="Test Item Data Bound" /><br>
</headertemplate>
<itemtemplate>
<asp:ImageButton ID="edittemplate" CommandName="EditTemplate" runat="server" Visible="false" ImageUrl="images/view.jpg" CommandArgument='<%#DataBi nder.Eval( Container. DataItem, "EventID")%>' />
</itemtemplate>
</asp:DataList>
I have a LinkButton in the HeaderTemplate of a DataList.
When you click it, I want it to change the CommandName of a ImageButton in the same DataList Control located in it's ItemTemplate.
How do I do this in C#? I get an error on line 3 of this code.
Thanks for your help.
LPP
==========================
void TestDataBound (object sender, DataListCommandEventArgs e) {
ImageButton ib = e.Item.FindControl("editte
Response.Write(ib.CommandN
Response.End();
}
</script>
<html>
<body>
<form id="myform" name="myform" runat="server">
<asp:DataList ID="dl1" ShowHeader="true" runat="server" ExtractTemplateRows="false
<headertemplate>
<asp:LinkButton ID="linkbutton" runat="server" CommandName="Test" Text="Test Item Data Bound" /><br>
</headertemplate>
<itemtemplate>
<asp:ImageButton ID="edittemplate" CommandName="EditTemplate"
</itemtemplate>
</asp:DataList>
ASKER
Here is my solution to the actual question I had.
<script language="c#" runat="server">
public string sTextBox1, LinkButtonText;
public static string CommandName;
Hashtable hTable;
Hashtable hTable2;
void Page_Load() {
hTable = new Hashtable();
hTable2 = new Hashtable();
if (!IsPostBack) {
CommandName = "ShowJobListings";
LinkButtonText = CommandName;
hTable2.Add("EventID", "6");
dl1.DataSource = hTable2;
dl1.Visible = true;
Page.DataBind();
}
}
void TestDataBound (object sender, DataListCommandEventArgs e) {
switch (e.CommandName) {
case "SwitchMode":
// hTable = new Hashtable();
switch (CommandName) {
case "ShowTemplates":
CommandName = "ShowJobListings";
LinkButtonText = CommandName;
hTable.Add("EventID", "7");
break;
case "ShowJobListings":
CommandName = "ShowTemplates";
LinkButtonText = CommandName;
hTable.Add("EventID", "6");
break;
}
dl1.DataSource = hTable;
dl1.Visible = true;
Page.DataBind();
break;
case "ShowTemplates":
Response.Write(e.CommandAr gument + " " );
Response.Write(e.CommandNa me);
Response.End();
break;
case "ShowJobListings":
Response.Write(e.CommandAr gument + " " );
Response.Write(e.CommandNa me);
Response.End();
break;
default:
Response.Write("error");
break;
}
}
</script>
<html>
<body>
<form id="myform" name="myform" runat="server">
<asp:DataList ID="dl1" ShowHeader="true" runat="server" ExtractTemplateRows="false " OnItemCommand="TestDataBou nd">
<headertemplate>
<asp:LinkButton ID="linkbutton" runat="server" CommandName="SwitchMode">
<%#CommandName%>
</asp:LinkButton><br>
</headertemplate>
<itemtemplate>
<asp:ImageButton ID="edittemplate" CommandName='<%#CommandNam e%>' runat="server" ImageUrl="images/view.jpg" CommandArgument='<%#DataBi nder.Eval( Container. DataItem, "Value")%>' AlternateText='<%#CommandN ame%>' />
</itemtemplate>
</asp:DataList>
<script language="c#" runat="server">
public string sTextBox1, LinkButtonText;
public static string CommandName;
Hashtable hTable;
Hashtable hTable2;
void Page_Load() {
hTable = new Hashtable();
hTable2 = new Hashtable();
if (!IsPostBack) {
CommandName = "ShowJobListings";
LinkButtonText = CommandName;
hTable2.Add("EventID", "6");
dl1.DataSource = hTable2;
dl1.Visible = true;
Page.DataBind();
}
}
void TestDataBound (object sender, DataListCommandEventArgs e) {
switch (e.CommandName) {
case "SwitchMode":
// hTable = new Hashtable();
switch (CommandName) {
case "ShowTemplates":
CommandName = "ShowJobListings";
LinkButtonText = CommandName;
hTable.Add("EventID", "7");
break;
case "ShowJobListings":
CommandName = "ShowTemplates";
LinkButtonText = CommandName;
hTable.Add("EventID", "6");
break;
}
dl1.DataSource = hTable;
dl1.Visible = true;
Page.DataBind();
break;
case "ShowTemplates":
Response.Write(e.CommandAr
Response.Write(e.CommandNa
Response.End();
break;
case "ShowJobListings":
Response.Write(e.CommandAr
Response.Write(e.CommandNa
Response.End();
break;
default:
Response.Write("error");
break;
}
}
</script>
<html>
<body>
<form id="myform" name="myform" runat="server">
<asp:DataList ID="dl1" ShowHeader="true" runat="server" ExtractTemplateRows="false
<headertemplate>
<asp:LinkButton ID="linkbutton" runat="server" CommandName="SwitchMode">
<%#CommandName%>
</asp:LinkButton><br>
</headertemplate>
<itemtemplate>
<asp:ImageButton ID="edittemplate" CommandName='<%#CommandNam
</itemtemplate>
</asp:DataList>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
{
ImageButton ib = e.Item.FindControl("editte
if (ib != null)
// do stuff here...
}