如果某个用户是第一次写入数据库,那么在页面上不会有数据显示.
必须再次打开页面才能显示数据.此时再添加数据就能正常显示了.
 //写入User_product_more 用户商品详细表,
                                DateTime startime = DateTime.Now;
....
upmta.InsertUserProduct(...,startime,...)//写入数据库 ContextUtil.SetComplete();//提交事务
//帮定数据
this.odsUPmore.SelectMethod = "GetAllKeywordByUin";
                                this.odsUPmore.SelectParameters.Clear();
                                this.odsUPmore.SelectParameters.Add("uin", System.TypeCode.Int32, this.lbUin.Text);
                                this.dlKeyword.DataBind();
//Response.Redirect("managekeyword.aspx");//再次打开该页面,但是会出错
// Response.Write("<script>window.location.href='managekeyword.aspx';</script>");//用此脚本可以打开页面页面代码<asp:DataList ID="dlKeyword" runat="server" DataSourceID="odsUPmore" Width="770px"
                                        ShowHeader="False" DataKeyField="up_id" RepeatColumns="3" RepeatDirection="Horizontal"
                                        OnItemCommand="dlKeyword_EditCommand" OnPreRender="dlKeyword_PreRender">
                                        <ItemTemplate>
                                            <table border="0" cellpadding="0" cellspacing="0" style="text-align: left; vertical-align: top;">
                                                <%-- width: 250px;--%>
                                                <tr>
                                                    <td style="width: 130px">
                                                        <asp:CheckBox ID="chkItem" runat="server"></asp:CheckBox>
                                                        <asp:Label ID="lbKeyword" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "up_name") %>'
                                                            Font-Size="12px"></asp:Label>
                                                        <%--<%#   DataBinder.Eval(Container.DataItem, "up_name")%>--%>
                                                    </td>
                                                    <td>
                                                        <a href="info_revise.aspx?up_id=<%# Eval("up_id") %>" style="font-size: 12px" title="单击修改详细信息">
                                                            发布供应</a>                                                    </td>
                                                </tr>
                                            </table>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Left" />
                                        <FooterTemplate>
                                            <%--&nbsp;&nbsp;&nbsp;&nbsp;--%>
                                            <asp:CheckBox ID="chkHeader" runat="server" AutoPostBack="False" onclick="javascript:SelectAll(this);"
                                                Text="全选/反选" /><%--//AutoPostBack设为假,不需要回发--%>
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            <asp:LinkButton ID="btDelete" runat="server" CommandName="delete" Text="删除" Visible="true"
                                                OnClientClick="return confirm('确定删除?');" />
                                        </FooterTemplate>
                                        <FooterStyle HorizontalAlign="Left" />
                                    </asp:DataList>
                                    <asp:ObjectDataSource ID="odsUPmore" runat="server" OldValuesParameterFormatString="original_{0}"
                                        TypeName="UserProductMoreTableAdapters.UPmoreProTableAdapter">
                                        <%--SelectMethod="GetAllKeywordByUin"--%>
                                        <SelectParameters>
                                            <asp:ControlParameter ControlID="lbUin" DefaultValue="0" Name="uin" PropertyName="Text"
                                                Type="Int32" />
                                        </SelectParameters>
                                    </asp:ObjectDataSource>
                                    <asp:Label ID="lbUin" runat="server" Visible="false"></asp:Label><!--接收后台的uin,传递给数据源,作为参数-->

解决方案 »

  1.   

    添加操作完成后
    把datalist的datasource重新绑定
      

  2.   

       
    dlKeyword.DataSourceID   =   "odsUPmore";       
       dlKeyword.DataBind();      
                             
      

  3.   

    两种方法:
    一,在添加完的事件后,用Response.Redirect("本页名");//重新Redirect本页;
    二,可以用Ajax;
      

  4.   

    把这断代码放到 if (!this.IsPostBack) 中
                {
                                                 DateTime startime = DateTime.Now;
    ....
    upmta.InsertUserProduct(...,startime,...)//写入数据库 ContextUtil.SetComplete();//提交事务
    //帮定数据
    this.odsUPmore.SelectMethod = "GetAllKeywordByUin";
                                    this.odsUPmore.SelectParameters.Clear();
                                    this.odsUPmore.SelectParameters.Add("uin", System.TypeCode.Int32, this.lbUin.Text);
                                    this.dlKeyword.DataBind();
                }
      

  5.   

    插入数据之后,对DataList进行重新绑定。
      

  6.   

    asmetoyou tang5399 你们是一个意思吧,我加了this.dlKeyword.DataSourceID = "odsUPmore";
    this.odsUPmore.SelectMethod = "GetAllKeywordByUin";
    this.odsUPmore.SelectParameters.Clear();
    this.odsUPmore.SelectParameters.Add("uin", System.TypeCode.Int32, this.lbUin.Text);
    this.dlKeyword.DataBind();
     
    照样不行.
      insus 我有写
      //Response.Redirect("managekeyword.aspx");//再次打开该页面,但是会出错
      Response.Write("<script>window.location.href='managekeyword.aspx';</script>");//用此脚本可以打开页面
    Ajax不会,要如何写?
     pxplyr_1984 我的代码是写在"protected void btnSubmit_Click(object sender, EventArgs e)"里的
    这个能写在if   (!this.IsPostBack)   中吗?不太明白.
      

  7.   

    wzy_love_sly 之前就是这样解决的,但觉得没必要--直接显示不是效率更高.
      

  8.   

    跟踪下此时odsUPmore中是否有数据
      

  9.   

    tang5399 加断点运行"this.dlKeyword.DataBind();"后调试"this.dlKeyword.Items.Count"的值是"1",表示有记录的.
    实际上如果是第二次打开页面或打开时Datalist就有数据显示,那么此时添加数据,在代码运行结束后就能在页面上显示了.
      

  10.   

    第一:你取出来的确实只有一条数据吗?
    第二:page_load里有什么代码
      

  11.   

    刚刚看到一篇文章说"Datalist不能自动创建行"难道是这个原因?
    我看了这个页面的源文件果然没有Datalist生成的<table>.
    这是有记录时页面的源文件<tr>
       <td style="width: 20px">
       </td>
       <td style="width: 770px" colspan="2" class="manage_lm_border">
          <span style="font-size: 14px">&nbsp;&nbsp;&nbsp;&nbsp;产品关键字</span> &nbsp;&nbsp;
          <table id="dlKeyword" cellspacing="0" border="0" style="width:770px;border-collapse:collapse;">这个就是Datalist生成的table
    <tr>
    <td align="left">
                <table border="0" cellpadding="0" cellspacing="0" style="text-align: left; vertical-align: top;">
                     <tr>
                         <td style="width: 130px">
                             <input id="dlKeyword_ctl00_chkItem" type="checkbox" name="dlKeyword$ctl00$chkItem" />
                                 <span id="dlKeyword_ctl00_lbKeyword" style="font-size:12px;">fgg</span>
                         </td>
                         <td>
                            <a href="info_revise.aspx?up_id=146253" style="font-size: 12px" title="单击修改详细信息">
                                 发布供应</a>&nbsp;
                         </td>
                      </tr>
                   </table>
                </td><td></td><td></td>
    </tr</table>
                                        
                                        <!--接收后台的uin,传递给数据源,作为参数-->
                                        
                                    </td>
                                </tr>
                                <!--现有关键字栏结束-->
    这个是添加完记录后的源文件(Datalist里有记录的)<tr>
        <td style="width: 20px">
        </td>
        <td style="width: 770px" colspan="2" class="manage_lm_border">
             <span style="font-size: 14px">&nbsp;&nbsp;&nbsp;&nbsp;产品关键字</span> &nbsp;&nbsp;<span id="lblEmpty">您尚未创建产品关键字</span>
                                        
                                        
                                        <!--接收后台的uin,传递给数据源,作为参数-->
                                        
                                    </td>
                                </tr>
                                <!--现有关键字栏结束-->
      

  12.   

    tang5399 看看有问题吗protected void Page_Load(object sender, EventArgs e)
    {
            int uin = 0;//用户编号
            GetUin getuin = new GetUin();
            if (getuin.GetName() != null && getuin.GetName().ToString().Trim() != "")
            {
                //返回uin,传给 控件的数据源 作为参数
                uin = getuin.GetUinByName(getuin.GetName().ToString().Trim());            this.lbUin.Text = uin.ToString().Trim();//传递给数据源,作为参数
                this.odsUPmore.SelectMethod = "GetAllKeywordByUin";
             }
    ...
    }
      

  13.   

    你省了些代码,也不用看了,先把page_load里的加上postback试试
    不能自动创建行云云没听过,至少我知道03是可以通过的,05不会越来越后退
      

  14.   

    继续讨论,下面是按tang5399 说的写的代码:if (this.IsPostBack)//点击"提交"按钮后先执行此代码,后执行写入数据库代码
            {
                this.dlKeyword.DataSourceID = "odsUPmore";
                this.odsUPmore.SelectMethod = "GetAllKeywordByUin";
                this.odsUPmore.SelectParameters.Clear();
                this.odsUPmore.SelectParameters.Add("uin", System.TypeCode.Int32, this.lbUin.Text);
                //this.dlKeyword.DataBind();//无论是否执行这行代码,Datalist里的记录数都不会改变,因为新记录尚未写入数据库            //判断有无关键字,并提示
                if (this.dlKeyword.Items.Count <= 0)
                {
                    this.dlKeyword.Visible = false;//此时Count为0,Datalist不可见.可问题是当运行完写入代码后,Datalist有记录了还是不可见
                    this.lblEmpty.Visible = true;
                }
                else
                {
                    this.dlKeyword.Visible = true;
                    this.lblEmpty.Visible = false;
                }
    }于是我在写入后加了以下代码upmta.InsertUserProduct(...);
    ContextUtil.SetComplete();//提交事务
    this.DataBind();
                                    
    this.dlKeyword.DataSourceID = "odsUPmore";
    this.odsUPmore.SelectMethod = "GetAllKeywordByUin";
    this.odsUPmore.SelectParameters.Clear();
    this.odsUPmore.SelectParameters.Add("uin", System.TypeCode.Int32, this.lbUin.Text);
    this.dlKeyword.DataBind();//判断有无关键字,并提示
    if (this.dlKeyword.Items.Count <= 0)
    {
         this.dlKeyword.Visible = false;
         this.lblEmpty.Visible = true;
    }
    else
    {
         this.dlKeyword.Visible = true;
         this.lblEmpty.Visible = false;
    }这样才行
      

  15.   

    因为原先我用了一个Datalist事件来判断是否在页面显示这个Datalist: protected void dlKeyword_PreRender(object sender, EventArgs e)
    {
            //判断有无关键字,并提示
            if (this.dlKeyword.Items.Count == 0)
            {
                this.dlKeyword.Visible = false;
                this.lblEmpty.Visible = true;
            }
            else
            {
                this.dlKeyword.Visible = true;
                this.lblEmpty.Visible = false;
            }
    }我原以为这个"dlKeyword_PreRender"可以在每次刷新后最后才执行到,那么就能判断出来是否显示了.
    结果发现提交后并不会执行这段代码.这就造成了如果Datalist原先不可见的话,那么提交数据后依然不可见.
      

  16.   

    继续
    现在必须在每次修改纪录后用这个代码才判断出是否显示Datalist //判断有无关键字,并提示
            if (this.dlKeyword.Items.Count == 0)
            {
                this.dlKeyword.Visible = false;
                this.lblEmpty.Visible = true;
            }
            else
            {
                this.dlKeyword.Visible = true;
                this.lblEmpty.Visible = false;
            }
    是否这么一个事件可以让我在页面上所有元素(各个控件,数据)都加载完成后在呈现给人看之前,使用上述办法来判断.
      

  17.   

    将新的数据源赋给Datalist
    然后重新绑定Datalist
      

  18.   

    这帖结了。不过没用所谓的“重新绑定”。我还是用了自己以前的方法,每次有增删后都用下面的代码判断。//判断有无关键字,并提示
            [align=left]if (this.dlKeyword.Items.Count == 0)
            {
                this.dlKeyword.Visible = false;
                this.lblEmpty.Visible = true;
            }
            else
            {
                this.dlKeyword.Visible = true;
                this.lblEmpty.Visible = false;
            }[/align]