开发者

Working with Datalist in asp.net?

good morning to all.

i place a datalist in my project in that i place a link button when i click on that link button a panel will open in that row with a textbox and a button. it is working fine but my problem is if i click on one link button of a row panel will open, when i click on second row link button the sencond row panel is opening but first row panel is not closing. I think you get my point owhter wise i will explain again this is my code check out

<form id="form1" runat="server">
<div>
<asp:DataList ID="Mydatalist" runat ="Server"
              OnItemCommand="Mydatalist_ItemCommand" >
  <ItemTemplate >
    <table >
      <tr>
        <td>
          <asp:Label ID="lblcouname" runat ="server"
                     Text ='<%# Eval("country_name") %>'></asp:Label>
        </td>
        <td> 
          <asp:LinkButton ID="lnkrepl" Text="reply"
                          CommandName ="reply" runat ="server"
                          CommandArgument ='<%# Eval("country_id") %>'>
          </asp:LinkButton>
        </td>
      </tr>
    </table>
    <div>
       <asp:Panel ID="mypane" runat ="Server" Visible ="false"  >
         <asp:TextBox ID="txtpane" runat ="Server" ></asp:TextBox><br />
         <asp:Button ID="btnInsert" runat="Server" Text ="Insert" />
       </asp:Panel>
    </div>
  </ItemTemplate>
</asp:DataList>
</div>
</form>

Code behind:

public partial class Datlist : System.Web.UI.Page
{
SqlConnection con; SqlDataAdapter da; DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
    con = new SqlConnection(
         @"server=msmsm;database=pop;user id=sa;password=abc");
    con.Open();
    if (!IsPostBack)
   开发者_开发知识库 {
        getCountry();
    }
}
public void getCountry()
{
    string sqr="select * from country";
    da=new SqlDataAdapter (sqr,con);
    ds = new DataSet();
    da.Fill(ds,"country");
    Mydatalist.DataSource = ds.Tables[0];
    Mydatalist.DataBind();
}
protected void Mydatalist_ItemCommand(object source, DataListCommandEventArgs e)
{
    Panel pn = (Panel)e.Item.FindControl("mypane");
    pn.Visible = false;
    if (e.CommandName == "reply")
    {

        pn.Visible = true;
    }
}
}


You have to programmably hide it. THe DataList should have an items property, and as such you can loop through all items, find the panel control using FIndControl, and set its visibility to false.

EDIT: So you need to do:

private void HideItems()
{
    foreach (var item in this.dl.Items)
    {
       var panel = item.FindControl("mypane") as Panel;
       if (panel != null)
           panel.Visible = false;
     }
}

In ItemCommand, call this method to hide all the existing control's panels.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜