传递参数从的ImageButton至code背后 [英] passing a parameter from a ImageButton to code behind
问题描述
EDITED问题
我想传递一个变量,在这种情况下,名称包含字符串法案,背后方法code和使用debug.print看到它
字符串名称=埋单;
< ASP:ImageButton的=服务器ID =DeleteButton的ImageUrl =图片/ delete.jpg
CommandArgument ='<%#的eval(name)的%GT;'的CommandName =ThisBtnClick文本=删除我的onclick =DeleteMonth/>
我试过:
CommandArgument ='<%#的eval(name)的%GT;'
CommandArgument ='<%#绑定(名称)%GT;'
CommandArgument ='<%=名%GT;
这是我的打印功能。
保护无效DeleteMonth(对象发件人,EventArgs的发送)
{
的ImageButton BTN =(ImageButton的)寄件人;
开关(btn.CommandName)
{
案ThisBtnClick:
Debug.Print( - + btn.CommandArgument.ToString());
打破;
案ThatBtnClick:
打破;
}
}
我想我需要先进行数据绑定的东西,但老实说,我不知道。我只是得到了一个空白,当我打印。有没有人处理,在此之前
修改
我想要做的是动态创建一个表什么。名义变量被从数据库中抽取,然后创建基于该名称的表。我想表的最后一栏是一个X,所以我可以删除该行的一切。我使用的ImageButton。这里是我的code
使用(康涅狄格州System.Data.SqlClient.SqlConnection =新System.Data.SqlClient.SqlConnection(sqlConnectionString))
{
conn.Open();
查询字符串=SELECT * FROM dbo.Archive
System.Data.SqlClient.SqlCommand CMD =新System.Data.SqlClient.SqlCommand(查询,康涅狄格州);
System.Data.SqlClient.SqlDataReader RDR = cmd.ExecuteReader(); 而(rdr.Read())
{
字符串名称,创建1Updated,2Updated,3Updated;
名称= rdr.GetString(1); //月份的名字
如果(!(rdr.IsDBNull(2)))//检查日期为空值
{创建= rdr.GetDateTime(2)的ToString(); } //如果不为null日期变成了串并保存在一个变量
其他
{创建=---; } //其他日期给出一个默认值
如果(!(rdr.IsDBNull(3)))
{1Updated = rdr.GetDateTime(3)的ToString(); }
其他
{1Updated =---; }
如果(!(rdr.IsDBNull(4)))
{2Updated = rdr.GetDateTime(4)的ToString(); }
其他
{2Updated =---; }
如果(!(rdr.IsDBNull(5)))
{3Updated = rdr.GetDateTime(5)的ToString(); }
其他
{3Updated =---; } 回复于(&所述; TR>&下; TD>中+名称+&下; / TD>中);
回复于(&所述; TD>中+创建+&下; / TD>中);
回复于(&所述; TD>中+ 1Updated +&下; / TD>中);
回复于(&所述; TD>中+ 2Updated +&下; / TD>中);
回复于(&所述; TD>中+ 3Updated +&下; / TD>中);
回复于(&所述; TD>&所述a取代1所述; / A>&下; / TD>中);
回复于(&所述; TD>&所述a取代; 2'; / A>&下; / TD>中);
回复于(&所述; TD>&所述a取代; 3'; / A>&下; / TD>中);
回复于(&所述; TD>&所述a取代;遵守总结&下; / A>&下; / TD>中); 的Response.Write(< TD ALIGN =中心>中+名);%GT;
< ASP:ImageButton的=服务器ID =DeleteButton的ImageUrl =图片/ delete.jpg
CommandArgument ='<%#的eval(name)的%GT;'的CommandName =ThisBtnClick文本=删除我的onclick =DeleteMonth/>
&所述;%回复于(&下; / TD>&下; / TR>中);
}
使用这样的......
< ASP:ImageButton的=服务器ID =DeleteButton的ImageUrl =图片/ delete.jpg
CommandArgument ='<%#的eval(用户名)%>'文字=删除我的onclick =DeleteMonth/> 保护无效DeleteMonth(对象发件人,EventArgs的发送)
{**的ImageButton BTN =((ImageButton的)寄件人).CommandArgument **
开关(btn.ToString())
{
案ThisBtnClick:
Debug.Print( - + btn.CommandArgument.ToString());
打破;
案ThatBtnClick:
打破;
}
}
EDITED QUESTION
I want to pass a variable, in this case 'name' containing the string bill, to a code behind method and use debug.print to see it
string name = "bill";
<asp:ImageButton runat="server" id="DeleteButton" ImageUrl="Images/delete.jpg"
CommandArgument='<%# Eval("name") %>' CommandName="ThisBtnClick" text="Delete Me" onclick="DeleteMonth" />
I've tried:
CommandArgument='<%# Eval("name") %>'
CommandArgument='<%# Bind("name") %>'
CommandArgument='<%= "name" %>
This is my print function
protected void DeleteMonth(object sender, EventArgs e)
{
ImageButton btn = (ImageButton)sender;
switch (btn.CommandName)
{
case "ThisBtnClick":
Debug.Print("--" + btn.CommandArgument.ToString());
break;
case "ThatBtnClick":
break;
}
}
I think i need to databind something first but honestly I have no idea. I just get a blank when i print. Has anyone dealt with this before
EDIT
What I want to do is dynamically create a table. the name variable gets pulled from a database and then creates a table based on that name. I want the last column of the table to be an X so i can delete everything in that row. I'm using an imagebutton. here's my code
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(sqlConnectionString))
{
conn.Open();
string query = "SELECT * FROM dbo.Archive";
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query, conn);
System.Data.SqlClient.SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
string name, created, 1Updated, 2Updated, 3Updated;
name = rdr.GetString(1); // name of month
if (!(rdr.IsDBNull(2))) // checks date for a null value
{ created = rdr.GetDateTime(2).ToString(); }// if not null date is turned into a string and saved in a variable
else
{ created = "---"; } // else date is given a default value
if (!(rdr.IsDBNull(3)))
{ 1Updated = rdr.GetDateTime(3).ToString(); }
else
{ 1Updated = "---"; }
if (!(rdr.IsDBNull(4)))
{ 2Updated = rdr.GetDateTime(4).ToString(); }
else
{ 2Updated = "---"; }
if (!(rdr.IsDBNull(5)))
{ 3Updated = rdr.GetDateTime(5).ToString(); }
else
{ 3Updated = "---"; }
Response.Write("<tr><td>" + name + "</td>");
Response.Write("<td>" + created + "</td>");
Response.Write("<td>" + 1Updated + "</td>");
Response.Write("<td>" + 2Updated + "</td>");
Response.Write("<td>" + 3Updated + "</td>");
Response.Write("<td><a>1</a></td>");
Response.Write("<td><a>2</a></td>");
Response.Write("<td><a>3</a></td>");
Response.Write("<td><a>Compliance Summary</a></td>");
Response.Write("<td align = 'center'>"+name);%>
<asp:ImageButton runat="server" id="DeleteButton" ImageUrl="Images/delete.jpg"
CommandArgument='<%# Eval("name") %>' CommandName="ThisBtnClick" text="Delete Me" onclick="DeleteMonth" />
<% Response.Write("</td></tr>");
}
Use like this......
<asp:ImageButton runat="server" id="DeleteButton" ImageUrl="Images/delete.jpg"
CommandArgument='<%# Eval("UserName")%>' text="Delete Me" onclick="DeleteMonth" />
protected void DeleteMonth(object sender, EventArgs e)
{
**ImageButton btn = ((ImageButton)sender).CommandArgument;**
switch (btn.ToString())
{
case "ThisBtnClick":
Debug.Print("--" + btn.CommandArgument.ToString());
break;
case "ThatBtnClick":
break;
}
}
这篇关于传递参数从的ImageButton至code背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!